20 Best SQL Query Interview Questions with Answers 2020

20 Best SQL Query Interview Questions with Answers 2020

Table – EmpDet                                                                             

E_ID F_Name M_Id DOJ
1 Sachin 101 01/12/2019
2 Rohit 201 01/10/2020
3 Virat 301 27/11/2020

 

Table – EmpSal

E_ID Project Salary
1 P1 15000
2 P2 26000
3 P1 27000

 

Ques.1. Write an SQL query to retrieve the count of employees who is working in project ‘P1’.

Ans. User shall use aggregate function count() with the SQL where clause-

SELECT COUNT(*) FROM EmpSal WHERE Project = 'P1';

Ques.2. Write a SQL query to fetch employee names having a salary greater than or equal to 15000 and less than or equal 20000.

Ans. The query will be  BETWEEN in the ‘where’ clause to return the E_ID of the employees with salary satisfying the required criteria and then use it as a subquery to find the F_Name of the employee form EmpDet table.

SELECT F_Name
FROM EmpDet
WHERE E_ID IN
(SELECT E_ID FROM  EmpSal
WHERE Salary BETWEEN 15000 AND 20000);

Ques.3. Write a SQL query to fetch project-wise count of employees sorted by project’s count in descending order.

Ans. This query has two requirements – first to fetch the project-wise count and then to sort the result by that count. For project-wise count, the query will be  GROUP BY clause and for sorting, and ORDER BY clause on the alias of the project-count.

SELECT Project, count(E_ID) EmpProjectCount
FROM EmpSal
GROUP BY Project
ORDER BY EmpProjectCount DESC;

Ques.4. Write a query to fetch only the first name(string before space) from the F_Name column of EmpDet table.

Ans. The user is required to first fetch the location of the space character in the F_Name field and then extract the first name out of the F_Name field. For finding the location, the query would be the LOCATE method in MySQL and CHARINDEX in SQL SERVER, and for fetching the string before space, a query would be SUBSTRING OR MID method.

mySQL- Using MID
SELECT MID(F_Name, 0, LOCATE(' ',F_Name)) FROMEmpDet;
SQL Server-Using SUBSTRING
SELECT SUBSTRING(F_Name, 0, CHARINDEX(' ',F_Name)) FROMEmpDet;

Also, the query would believe which returns the left part of a string till the specified number of characters.

SELECT LEFT(F_Name, CHARINDEX(' ',F_Name) - 1) FROMEmpDet;

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, the query would believe join with an EmployeeDetail table on the left side.

SELECT E.F_Name, S.Salary 
FROMEmpDet E LEFT JOIN EmpSal S
ON E.E_ID = S.E_ID;

Ques.6. Write a SQL query to fetch all the Employees who are also managers fromEmpDet table.

Ans. Here, we have to use Self-Join as the requirement wants us to analyze theEmpDet table as two different tables, each for Employee and manager records.

SELECT DISTINCT E.F_Name
FROM EmpDetails E
INNER JOIN EmpDetails M
ON E.E_ID = M.M_Id;

Ques.7. Write a SQL query to fetch all employee records fromEmpDet table who have a salary record in EmpSal table.

Ans. Using ‘Exists’-

SELECT * FROMEmpDet E
WHERE EXISTS
(SELECT * FROM EmpSal S WHERE  E.E_ID = S.E_ID);

Ques.8. Write a SQL query to fetch duplicate records from a table.

Ans. To find duplicate records from table query would be GROUP BY on all the fields and then use HAVING clause to return only those fields whose count is greater than one, i.e., the rows having duplicate records.

SELECT E_ID, Project, Salary, COUNT(*)
FROM EmpSal
GROUP BY E_ID, Project, Salary
HAVING COUNT(*) > 1;

Ques.9. Write a SQL query to remove duplicates from a table without using a temporary table.

Ans. Using Group By and Having clause-

DELETE FROM EmpSal 
WHERE E_ID IN (
SELECT E_ID
FROM EmpSal      
GROUP BY Project, Salary
HAVING COUNT(*) > 1));

Using rowId in Oracle-

DELETE FROM EmpSal
WHERE rowid NOT IN
(SELECT MAX(rowid) FROM EmpSal GROUP BY E_ID);

Ques.10. Write a SQL query to fetch only odd rows from the table.

Ans. This can be achieved by using Row_number in SQL server-

SELECT E.E_ID, E.Project, E.Salary
FROM (
    SELECT *, Row_Number() OVER(ORDER BY E_ID) AS RowNumber
    FROM EmpSal
) E
WHERE E.RowNumber % 2 = 1

Ques.11. Write a SQL query to fetch only even rows from the table.

Ans. Using the same Row_Number() and checking that the remainder, when divided by 2, is 0-

SELECT E.E_ID, E.Project, E.Salary
FROM (
    SELECT *, Row_Number() OVER(ORDER BY E_ID) AS RowNumber
    FROM EmpSal
) E
WHERE E.RowNumber % 2 = 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-

SELECT * INTO newTable FROMEmpDet;

Ques.13. Write an SQL query to create an empty table with the same structure as some other table.

Ans. Using SELECT INTO command with False ‘WHERE’ condition-

SELECT * INTO newTable FROMEmpDet WHERE 1 = 0;

This can also do using MySQL ‘Like’ command with CREATE statement-

CREATE TABLE newTable LIKEEmpDet;

Ques.14. Write a SQL query to fetch common records between two tables.

Ans. Using INTERSECT-

SELECT * FROM EmpSal
INTERSECT
SELECT * FROM ManagerSalary

Ques.15. Write a SQL query to fetch records that are present in one table but not in another table.

Ans. Using MINUS-

SELECT * FROM EmpSal
MINUS
SELECT * FROM ManagerSalary

Ques.16. Write a SQL query to find current date-time.

Ans. mySQL-

SELECT NOW();

SQL Server-

SELECT getdate();

Oracle-

SELECT SYSDATE FROM DUAL;

Ques.17. Write a SQL query to fetch all the Employees fromEmpDet table who joined in the Year 2020.

Ans. Using BETWEEN for the date range ’01-01-2020 AND ’31-12-2020′-

SELECT * FROM EmpSal

WHERE DOJ BETWEEN '01-01-2020' AND date '31-12-2020';

Also, we can extract the year part of the joining date (using YEAR in MySQL)-

SELECT * FROM EmpSal
WHERE YEAR(DOJ) = '2020';

Ques.18. Write a SQL query to fetch top n records?

Ans. In MySQL using LIMIT-

SELECT * FROM EmpSal ORDER BY Salary DESC LIMIT N

In SQL server using TOP command-

SELECT TOP N * FROM EmpSal ORDER BY Salary DESC

In Oracle using ROWNUM-

SELECT * FROM (SELECT * FROM EmpSal ORDER BY Salary DESC)

WHERE ROWNUM <= 3;

Ques.19. Write SQL query to find the nth highest salary from the table.

Ans. Using Top keyword (SQL Server)-

SELECT TOP 1 Salary
FROM (
      SELECT DISTINCT TOP N Salary
      FROM Employee
      ORDER BY Salary DESC
      )
ORDER BY Salary ASC

Using limit clause(mySQL)-

SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT N-1,1;

Ques.20. Write SQL query to find the 3rd highest salary from the table without using TOP/limit keyword.

Ans. The below SQL query makes use of correlated subquery wherein 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.

SELECT Salary
FROM EmpSal Emp1
WHERE 2 = (
                SELECT COUNT( DISTINCT ( Emp2.Salary ) )
                FROM EmpSal Emp2
                WHERE Emp2.Salary >= Emp1.Salary
            )

For nth highest salary-

SELECT Salary
FROM EmpSal Emp1
WHERE N-1 = (
                SELECT COUNT( DISTINCT ( Emp2.Salary ) )
                FROM EmpSal Emp2
                WHERE Emp2.Salary >= Emp1.Salary
            )

Leave a Reply

Close Menu