Skip to main content

SQL Interview Questions

SQL stands for Structured Query Language , and it is used to communicate with the Database. This is a standard language used to perform tasks such as retrieval, updation, insertion and deletion of data from a database.

Q1. What is the difference between DELETE and TRUNCATE statements?

Delete  :-
  1. Delete command is used to delete a row in a table.
  2. You can rollback data after using delete statement.t is a DML command.
  3. It is slower than truncate statement.

TRUNCATE

  1. Truncate is used to delete all the rows from a table.
  2. You cannot rollback data.
  3. It is a DDL command.It is faster.

Q2. What are the different subsets of SQL?

  • DDL (Data Definition Language) – It allows you to perform various operations on the database such as CREATE, ALTER and DELETE objects.
  • DML ( Data Manipulation Language) – It allows you to access and manipulate data. It helps you to insert, update, delete and retrieve data from the database.
  • DCL ( Data Control Language) – It allows you to control access to the database. Example – Grant, Revoke access permissions.

3. What are different Clauses used in SQL?


WHERE Clause: This clause is used to define the condition, extract and display only those records which fulfill the given condition


Syntax: SELECT column_name(s)
FROM table_name
WHERE condition

SELECT * FROM Customers
WHERE Country='India';

GROUP BY Clause: It is used with SELECT statement to group the result of the executed query using the value specified in it. It matches the value with the column name in tables and groups the end result accordingly.

Syntax: SELECT column_name(s)
FROM table_name
GROUP BY column_name;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID)
HAVING clause: This clause is used in association with the GROUP BY clause. It is applied to each group of result or the entire result as a single group and much similar as WHERE clause, the only difference is you cannot use it without GROUP BY clause
Syntax: SELECT column_name(s)
FROM table_name
GROUP BY column_name
HAVING condition;
The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

ORDER BY clause: This clause is to define the order of the query output either in ascending (ASC) or in descending (DESC) order. Ascending (ASC) is the default one but descending (DESC) is set explicitly.
Syntax: SELECT column_name(s)
FROM table_name
WHERE condition
ORDER BY column_name ASC|DESC;
SELECT * FROM Customers
ORDER BY Country;
SELECT * FROM Customers
ORDER BY Country DESC;

USING clause: USING clause comes in use while working with SQL Joins. It is used to check equality based on columns when tables are joined. It can be used instead ON clause in Joins.
Syntax: SELECT column_name(s)
FROM table_name
JOIN table_name
USING (column_name);



4. SQL JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:
  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table

5. What are properties of the transaction?

Properties of transaction are known as ACID properties, such as
Atomicity: Ensures the completeness of all transactions performed. Checks whether every transaction is completed successfully if not then transaction is aborted at the failure point and the previous transaction is rolled back to its initial state as changes undone
Consistency: Ensures that all changes made through successful transaction are reflected properly on database
Isolation: Ensures that all transactions are performed independently and changes made by one transaction are not reflected on other
Durability: Ensures that the changes made in database with committed transactions persist as it is even after system failure



6. Aggregate functions we use in SQL
  • AVG(): Returns the average value from specified columns
  • COUNT(): Returns number of table rows
  • MAX(): Returns largest value among the records
  • MIN(): Returns smallest value among the records
  • SUM(): Returns the sum of specified column values
  • FIRST(): Returns the first value
  • LAST(): Returns Last value
7.  SQL Privileges?

SQL GRANT and REVOKE commands are used to implement privileges in SQL multiple user environments.  The administrator of the database can grant or revoke privileges to or from users of database object like SELECT, INSERT, UPDATE, DELETE, ALL etc.
GRANT Command: This command is used provide database access to user apart from an administrator.
REVOKE Command: This command is used provide database deny or remove access to database objects.


8. What is the difference between DROP and TRUNCATE?
TRUNCATE removes all rows from the table which cannot be retrieved back, DROP removes the entire table from the database and it cannot be retrieved back.
9.How to write a query to show the details of a student from Students table whose name starts with K?


SELECT * FROM Student WHERE Student_Name like ‘K%’;
Here ‘like’ operator is used for pattern matching.

10. SQL query to find second highest salary ?


Highest salary :-

SELECT name, MAX(salary) as salary FROM employee

second largest salary

SELECT name, MAX(salary) AS salary
 FROM employee
WHERE salary < (SELECT MAX(salary)
                FROM employee);


11. Select all records where Employee starts with ‘S’ and its length is 6 char.

select * from emp where Ename like'S____';

12. Select all records where Ename may be any no of character but it should end with ‘R’.

select * from emp where Ename like'%R';


13.Select all records from Employee table whose name is ‘Ashish’ and ‘Anuj’.


Select * from Employee where Name in(‘Amit’,’Pradnya’)


Comments

Popular posts from this blog

Handle radio button & Check Boxes in Selenium

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options. Selenium WebDriver supports Radio Button and Radio Group controls using the WebElement class. We can select and deselect the radio buttons using the click() method of the WebElement class and check whether a radio button is selected or deselected using the isSelected() method. Before performing the click on the Radio buttons or check boxes we will have to verify follwing scenarios :- If Radio button or Checkbox is displayed on the webpage If Radio button or Checkbox is enabled on the webpage Check the default selection of the Radio button or Checkbox We use predefined methods present in selenium to handle check Box and Radio  button :- isDisplayed() isEnabled() isSelected() 1.  isDisplayed () this method returns a Boolean value, if it returns t

How To Send Report In Mail Using Java In Selenium Web driver

Today we will share code how to send any type of selenium report in mail using java. For sending the email using JavaMail API, you need to load the two jar files: mail.jar activation.jar package SeleniumNew; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*;  import javax.mail.internet.*;  public class SendEmail {     public static void main(String[] args)     {                 String host="smtp.gmail.com";          final String user="ashishxx@gmail.com";//change accordingly          final String password="XXX";//change accordingly                    /*String[] to={"AshishXX@gmail.com","Ashishxxx@gmail.com"};           String[] cc={};           String[] bcc={};         */           String to="ashishxx@gmail.com";//change accordingly                     //Get the session object            Properties props = new

Appium architecture (How Appium works internally)

Appium Architecture  A ppium is a cross-platform automation tool, API of it supports both OS (Android and iOS) test scripts.It is tested on simulators (iOS,), emulators (Android), and real devices (iOS, Android)   Appium is an HTTP server written in Node.js that creates and handles WebDriver sessions.The Appium web server follows the same approach as the Selenium WebDriver, which receives HTTP requests from client libraries through JSON and then handles those requests in different ways   JSON wire protocol The JSON wire protocol  ( JSONWP ) is a transport mechanism created by WebDriver developers. This wire protocol is a specific set of predefined, standardized endpoints exposed via a RESTful API.   Appium implements the Mobile JSONWP, the extension to the Selenium JSONWP, and it controls the different mobile device behaviors, such as installing/uninstalling apps over the session Let’s have a look at some of the endpoints from the API which are used to interact with