Top 7 SQL Interview Questions and Answers

Here, you will come across some of the most frequently asked questions in SQL job interviews which will help you in your interview preparation.

Let's have a look at some of the most popular and significant SQL questions and answers:

Interview Questions




Most Essential And Frequently Asked Interview
Questions And Answer

SQL

Q.1. What do you mean by foreign key?

Ans.:

SQL | Foreign Key

A foreign key is a key used to link two tables. 

View Detials

Q.2. What is a view in SQL?

Ans.:

SQL | View

View in SQL is a kind of virtual table. It does not physical exists in our database but have rows and column as there we have in a real database table. we can create a view by selecting fields from one of the tables present in the database. A View can have all the rows of a table or specific rows based on certain conditions.

View Detials

Q.3. What is the difference between primary key and unique constraints?

Ans.:

SQL | Difference between primary key and unique constraints

There is only one primary key in a table. It creates the clustered index automatically and it cannot have NULL value
whereas there can be multiple unique constraints in a table. It does not create the clustered index automatically and it can have a NULL value

Q.4. Write an SQL query to find names of employee start with 'A'?

Ans.:

SQL | LIKE operator

The LIKE operator of SQL is used for such kind of operations. It is used to fetch filtered data by searching for a particular pattern in where clause.

View Detials

Q.5. What is the difference between BETWEEN and IN operator in SQL?

Ans.:

SQL | difference between BETWEEN and IN operator

The BETWEEN operator selects all records between the given range whereas IN operator selects all matching records given with the WHERE clause.

View Detials

Q.6. Find nth largest salary from employees table?

Ans.:

By using following query we can find out the nth largest salary from employees table.

SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET n-1

In the above query n is replaced by any number where n >= 1. Suppose you want to find fifth highest salary then you have to use offset = 5-1 i.e.: 4

The query will be:

SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET 4

 

Q.7. How can we optimize a table?

Ans.:

OPTIMIZE command is use for optimize a table.

OPTIMIZE TABLE `users`