Hello friends! in this post we will see some of the most commonly asked SQL queries in interviews. The questions will start from very basic questions and then move to more complex problems. Consider the below two tables for most of the questions asked here.
Ques.1. Write a SQL query to fetch the count of employees working in project 'P1'.
Ans. Here, we would be using aggregate function count() with the SQL where clause-
Ques.2. Write a SQL query to fetch employee names having salary greater than or equal to 5000 and less than or equal 10000.
Ans. Here, we will use BETWEEN in the 'where' clause to return the empId of the employees with salary satifying the required criteria and then use it as subquery to find the fullName of the employee form EmployeeDetails table.
Ques.3. Write a SQL query to fetch project-wise count of employees sorted by project's count in descending order.
Ans. The query has two requirements - first to fetch the project-wise count and then to sort the result by that count. For project wise count, we will be using GROUPBY clause and for sorting, we will use ORDER BY clause on the alias of the project-count.
Ques.4. Write a query to fetch only the first name(string before space) from the FullName column of EmployeeDetails table.
Ans. In this question, we are required to first fetch the location of the space character in the FullName field and then extract the first name out of the FullName field. For finding the location we will use LOCATE method in mySQL and CHARINDEX in SQL SERVER and for fetching the string before space, we will use SUBSTRING OR MID method.
Ques.5. Write a query to fetch employee names and salary records. Return employee details even if the salary record is not present for the employee.
Ans. Here, we can use left join with EmployeeDetail table on the left side.
Ques.6. Write a SQL query to fetch all the Employees who are also managers from EmployeeDetails table.
Ans. Here, we have to use Self-Join as the requirement wants us to analyze the EmployeeDetails table as two different tables, each for Employee and manager records.
Ques.7. Write a SQL query to fetch all employee records from EmployeeDetails table who have a salary record in EmployeeSalary table.
Ans. Using 'Exists'-
Ques.8. Write a SQL query to fetch duplicate records from a table.
Ans. In order to find duplicate records from table we can use GROUP BY on all the fields and then use HAVING clause to return only those fields whose count is greater than 1 i.e. the rows having duplicate records.
Ques.9. Write a SQL query to remove duplicates from a table without using temporary table.
Ans. Using Group By and Having clause-
Using rowId in Oracle-
Ques.10. Write a SQL query to fetch only odd rows from table.
Ans. This can be achieved by using Row_number in SQL server-
Ques.11. Write a SQL query to fetch only even rows from table.
Ans. Using the same Row_Number() and checking that the remainder when divided by 2 is 0-
Ques.12. Write a SQL query to create a new table with data and structure copied from another table.
Ans. Using SELECT INTO command-
Ques.13. Write a SQL query to create an empty table with same structure as some other table.
Ans. Using SELECT INTO command with False 'WHERE' condition-
This can also done using mySQL 'Like' command with CREATE statement-
Ques.14. Write a SQL query to fetch common records between two tables.
Ans. Using INTERSECT-
Ques.15. Write a SQL query to fetch records that are present in one table but not in another table.
Ans. Using MINUS-
Ques.16. Write a SQL query to find current date-time.
Ques.17. Write a SQL query to fetch all the Employees details from EmployeeDetails table who joined in Year 2016.
Ans. Using BETWEEN for the date range '01-01-2016' AND '31-12-2016'-
Also, we can extract year part from the joining date (using YEAR in mySQL)-
Ques.18. Write a SQL query to fetch top n records?
Ans. In mySQL using LIMIT-
In SQL server using TOP command-
In Oracle using ROWNUM-
Ques.19. Write SQL query to find the nth highest salary from table.
Ans. Using Top keyword (SQL Server)-
Using limit clause(mySQL)-
Ques.20. Write SQL query to find the 3rd highest salary from table without using TOP/limit keyword.
Ans. The below SQL query make use of correlated subquery wherein in order to find the 3rd highest salary the inner query will return the count of till we find that there are two rows that salary greater than other distinct salaries.
For nth highest salary-