All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.


Total Pageviews

Blog Archive


Wednesday, 4 July 2018

SQL Query Interview Questions and Answers

In this article, we will see some of the common and important SQL queries asked in interviews whenever you will appear for any programming interview. We will start with basic SQL queries and then move to more complex Queries.

These SQL queries will test your skills on Joins both INNER and OUTER and filtering record using Having or Where clause, Grouping records by Group By Clause and calculating the sum, average, Counting numbers using aggregate functions and finding the top or Bottom elements using Order By clause.

Find more interview questions here-

SQL Query Interview Questions and Answers

Question 1: SQL query to find the number of employees according to gender whose DOB is between 01/01/1990 to 31/12/2005?

SELECT COUNT(*), sex from Employees WHERE DOB BETWEEN '01/01/1960' AND '31/12/1975' GROUP BY sex;

Question 2: SQL query to fetch records that are present in one table but not in another table?
Answer:  we will use MINUS to perform this operation - 


Question 3: SQL query to fetch common records from two tables?
Answer: we will use INTERSECT to find common records


Question 4: SQL query to create a new table with structure and data copied from another table?
Answer : 

SELECT * INTO NewTable FROM EmpTable;

Question 5: SQL query to find Nth highest salary?
Answer:  We can find Nth highest salary with this query alone.  We only need to change one value.

select * from Employee E where 2 = (select count( distinct E1.salary) from Employee E1 where E1.salary>E.salary); 

Question 6: SQL query to find and delete duplicate rows from SQL table?
Answer: For selecting distinct records use this SQL query - 

SELECT * FROM emp EMP1 WHERE rowid = (SELECT MAX(rowid) FROM emp EMP2  WHERE EMP1.empno = EMP2.empno);
To Delete

DELETE FROM emp EMP1 WHERE rowid != (SELECT MAX(rowid) FROM emp EMP2  WHERE EMP1.empno = EMP2.empno);

Question 7: SQL query to find all employees whose name starts with 'S'?

SELECT * FROM Employees WHERE EmpName like 'M%';

Question 8: SQL query to find second highest salary of employee?
Answer: There are multiple ways to answer this SQL query we can either use joins or ca write subquery. Here is subquery to find second highest salary of employee - 

select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee ); 

Question 9: SQL query to find employees whose salary is less than or equal to15000?

SELECT EmpName FROM  Employees WHERE  Salary<=15000;

Question 10: SQL query to find Top 3 salaries from Employee table?
Answer: we have an employee table with employee id and their salaries. we will find top 3 salaries by using subquery- 

SELECT Salary FROM (SELECT Salary FROM emp ORDER BY Salary desc)
WHERE rownum <= 3  ORDER BY Salary; 

0 on: "SQL Query Interview Questions and Answers"