SQL Interview Questions

There is given sql interview questions and answers that has been asked in many companies. For PL/SQL interview questions, visit our next page.

1) What is SQL?

SQL stands for structured query language. It is a database language used for database creation, deletion, fetching rows and modifying rows etc. sometimes it is pronounced as se-qwell.

2) When SQL appeared?

It appeared in 1974.

3) What are the usages of SQL?

  • To execute queries against a database
  • To retrieve data from a database
  • To inserts records in a database
  • To updates records in a database
  • To delete records from a database
  • To create new databases
  • To create new tables in a database
  • To create views in a database

4) Does SQL support programming?

No, SQL doesn't have loop or Conditional statement. It is used like commanding language to access databases.

5) What are the subsets of SQL?

  1. Data definition language (DDL)
  2. Data manipulation language (DML)
  3. Data control language (DCL)

6) What is data definition language?

Data definition language(DDL) allows you to CREATE, ALTER and DELETE database objects such as schema, tables, view, sequence etc.

7) What is data manipulation language?

Data manipulation language makes user able to access and manipulate data. It is used to perform following operations.

  • Insert data into database
  • Retrieve data from the database
  • Update data in the database
  • Delete data from the database

8) What is data control language?

Data control language allows you to control access to the database. It includes two commands GRANT and REVOKE.

GRANT: to grant specific user to perform specific task.

REVOKE: to cancel previously denied or granted permissions.

9) What are the type of operators available in SQL?

  1. Arithmetic operators
  2. Logical operators
  3. Comparison operator

10) What is the difference between clustered and non clustered index in SQL?

There are mainly two type of indexes in SQL, Clustered index and non clustered index. The differences between these two indexes is very important from SQL performance perspective.

1) One table can have only one clustered index but it can have many non clustered index. (approximately 250).

2) clustered index determines how data is stored physically in table. Actually clustered index stores data in cluster, related data is stored together so it makes simple to retrieve data.

3) reading from a clustered index is much faster than reading from non clustered index from the same table.

4) clustered index sort and store data rows in the table or view based on their key value, while non cluster have a structure separate from the data row.

11) What is the SQL query to display current date?

There is a built in function in SQL called GetDate() which is used to return current timestamp.

12) Which types of join is used in SQL widely?

The knowledge of JOIN is very necessary for an interviewee. Mostly used join is INNER JOIN and (left/right) OUTER JOIN.

13) What is "TRIGGER" in SQL?

Trigger allows you to execute a batch of SQL code when an insert, update or delete command is executed against a specific table.

Actually triggers are special type of stored procedures that are defined to execute automatically in place or after data modifications.

14) What is self join and what is the requirement of self join?

Self join is often very useful to convert a hierarchical structure to a flat structure. It is used to join a table to itself as like if that is the second table.

15) What are set operators in SQL?

Union, intersect or minus operators are called set operators.

16) What is a constraint? Tell me about its various levels.

Constraints are representators of a column to enforce data entity and consistency. There are two levels :

  1. column level constraint
  2. table level constraint

17) Write an SQL query to find names of employee start with 'A'?

  1. SELECT * FROM Employees WHERE EmpName like 'A%'  

18) Write an SQL query to get third maximum salary of an employee from a table named employee_table.

  1. SELECT TOP 1 salary   
  2. FROM (  
  3. SELECT TOP 3 salary  
  4. FROM employee_table  
  5. ORDER BY salary DESC ) AS emp  
  6. ORDER BY salary ASC;      

19) What is the difference between DELETE and TRUNCATE statement in SQL?

The main differences between SQL DELETE and TRUNCATE statements are given below:

1)DELETE is a DML command.TRUNCATE is a DDL command.
2)We can use WHERE clause in DELETE command.We cannot use WHERE clause with TRUNCATE
3)DELETE statement is used to delete a row from a tableTRUNCATE statement is used to remove all the rows from a table.
4)DELETE is slower than TRUNCATE statement.TRUNCATE statement is faster than DELETE statement.
5)You can rollback data after using DELETE statement.It is not possible to rollback after using TRUNCATE statement.

20) What is ACID property in database?

ACID property is used to ensure that the data transactions are processed reliably in a database system.

A single logical operation of a data is called transaction.

ACID is an acronym for Atomicity, Consistency, Isolation, Durability.

Atomicity: it requires that each transaction is all or nothing. It means if one part of the transaction fails, the entire transaction fails and the database state is left unchanged.

Consistency: the consistency property ensure that the data must meet all validation rules. In simple words you can say that your transaction never leaves your database without completing its state.

Isolation: this property ensure that the concurrent property of execution should not be met. The main goal of providing isolation is concurrency control.

Durability: durability simply means that once a transaction has been committed, it will remain so, come what may even power loss, crashes or errors.