Must-Know Top 30 SQL Query Interview Questions and Answers in 2024

Are you preparing for an upcoming SQL interview? If yes, then you’ve come to the right place! SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases. It’s an essential skill for roles like Database Administrator, Data Analyst, and Business Intelligence Developer.

In this comprehensive blog post, we’ll cover the top 30 SQL query interview questions and answers that will help you ace your next interview. Let’s dive in!

1. Write a query to fetch the EmpFname from the EmployeeInfo table in uppercase and use the alias name as EmpName.

sql

SELECT UPPER(EmpFname) AS EmpName FROM EmployeeInfo;

2. Write a query to fetch the number of employees working in the ‘HR’ department.

sql

SELECT COUNT(*) FROM EmployeeInfo WHERE Department = 'HR';

3. Write a query to get the current date.

In SQL Server:

sql

SELECT GETDATE();

In MySQL:

sql

SELECT SYSTDATE();

4. Write a query to retrieve the first four characters of EmpLname from the EmployeeInfo table.

sql

SELECT SUBSTRING(EmpLname, 1, 4) FROM EmployeeInfo;

5. Write a query to fetch only the place name (string before brackets) from the Address column of the EmployeeInfo table.

Using the MID function in MySQL:

sql

SELECT MID(Address, 0, LOCATE('(',Address)) FROM EmployeeInfo;

Using SUBSTRING in SQL Server:

sql

SELECT SUBSTRING(Address, 1, CHARINDEX('(',Address)) FROM EmployeeInfo;

6. Write a query to create a new table with the same data and structure as another table.

Using the SELECT INTO command in SQL Server:

sql

SELECT * INTO NewTable FROM EmployeeInfo WHERE 1 = 0;

Using the CREATE TABLE command in MySQL:

sql

CREATE TABLE NewTable AS SELECT * FROM EmployeeInfo;

7. Write a query to find all employees whose salary is between 50,000 and 100,000.

sql

SELECT * FROM EmployeePosition WHERE Salary BETWEEN 50000 AND 100000;

8. Write a query to find the names of employees whose first name begins with ‘S’.

sql

SELECT * FROM EmployeeInfo WHERE EmpFname LIKE 'S%';

9. Write a query to fetch the top N records.

Using the TOP command in SQL Server:

sql

SELECT TOP N * FROM EmployeePosition ORDER BY Salary DESC;

Using the LIMIT command in MySQL:

sql

SELECT * FROM EmployeePosition ORDER BY Salary DESC LIMIT N;

10. Write a query to retrieve the EmpFname and EmpLname in a single column as “FullName”. The first and last names should be separated by a space.

sql

SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmployeeInfo;

11. Write a query to find the number of employees whose date of birth is between 05/02/1970 and 12/31/1975, grouped by gender.

sql

SELECT COUNT(*), Gender FROM EmployeeInfo WHERE DOB BETWEEN '1970-02-05' AND '1975-12-31'GROUP BY Gender;

12. Write a query to fetch all records from the EmployeeInfo table, ordered by EmpLname in descending order and Department in ascending order.

sql

SELECT * FROM EmployeeInfoORDER BY EmpLname DESC, Department ASC;

13. Write a query to fetch details of employees whose last name ends with ‘a’ and contains five letters.

sql

SELECT *FROM EmployeeInfoWHERE EmpLname LIKE '____a';

14. Write a query to fetch details of all employees excluding those with the first names ‘Sanjay’ and ‘Sonia’ from the EmployeeInfo table.

sql

SELECT *FROM EmployeeInfoWHERE EmpFname NOT IN ('Sanjay', 'Sonia');

15. Write a query to fetch details of employees with the address as ‘DELHI(DEL)’.

sql

SELECT *FROM EmployeeInfoWHERE Address LIKE 'DELHI(DEL)%';

16. Write a query to fetch all employees who also hold a managerial position.

sql

SELECT E.EmpFname, E.EmpLname, P.EmpPositionFROM EmployeeInfo EINNER JOIN EmployeePosition P ON E.EmpID = P.EmpIDAND P.EmpPosition = 'Manager';

17. Write a query to fetch the department-wise count of employees sorted by the department’s count in ascending order.

sql

SELECT Department, COUNT(EmpID) AS EmpDeptCountFROM EmployeeInfoGROUP BY DepartmentORDER BY EmpDeptCount ASC;

18. Write a query to calculate the even and odd records from a table.

To retrieve even records:

sql

SELECT EmpIDFROM (SELECT ROW_NUMBER() OVER (ORDER BY EmpID) AS RowNo, EmpID FROM EmployeeInfo)WHERE MOD(RowNo, 2) = 0;

To retrieve odd records:

sql

SELECT EmpID FROM (SELECT ROW_NUMBER() OVER (ORDER BY EmpID) AS RowNo, EmpID FROM EmployeeInfo)WHERE MOD(RowNo, 2) = 1;

19. Write a SQL query to retrieve employee details from the EmployeeInfo table who have a date of joining in the EmployeePosition table.

sql

SELECT *FROM EmployeeInfo EWHERE EXISTS (    SELECT *    FROM EmployeePosition P    WHERE E.EmpID = P.EmpID);

20. Write a query to retrieve the two minimum and two maximum salaries from the EmployeePosition table.

To retrieve two minimum salaries:

sql

SELECT DISTINCT SalaryFROM EmployeePosition E1WHERE 2 >= (    SELECT COUNT(DISTINCT Salary)    FROM EmployeePosition E2    WHERE E1.Salary >= E2.Salary)ORDER BY E1.Salary ASC;

To retrieve two maximum salaries:

sql

SELECT DISTINCT SalaryFROM EmployeePosition E1WHERE 2 >= (    SELECT COUNT(DISTINCT Salary)    FROM EmployeePosition E2    WHERE E1.Salary <= E2.Salary)ORDER BY E1.Salary DESC;

21. Write a query to find the Nth highest salary from the table without using the TOP/LIMIT keyword.

sql

SELECT SalaryFROM EmployeePosition E1WHERE N-1 = (    SELECT COUNT(DISTINCT Salary)    FROM EmployeePosition E2    WHERE E2.Salary > E1.Salary);

22. Write a query to retrieve duplicate records from a table.

sql

SELECT EmpID, EmpFname, Department, COUNT(*)FROM EmployeeInfoGROUP BY EmpID, EmpFname, DepartmentHAVING COUNT(*) > 1;

23. Write a query to retrieve the list of employees working in the same department.

sql

SELECT DISTINCT E.EmpID, E.EmpFname, E.DepartmentFROM EmployeeInfo E, EmployeeInfo E1WHERE E.Department = E1.DepartmentAND E.EmpID != E1.EmpID;

24. Write a query to retrieve the last 3 records from the EmployeeInfo table.

sql

SELECT *FROM EmployeeInfoWHERE EmpID IN (    SELECT EmpID    FROM (        SELECT EmpID, ROW_NUMBER() OVER (ORDER BY EmpID DESC) AS RowNum        FROM EmployeeInfo    ) AS subquery    WHERE RowNum <= 3);

25. Write a query to find the third-highest salary from the EmployeePosition table.

sql

SELECT SalaryFROM (    SELECT Salary, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum    FROM EmployeePosition) AS subqueryWHERE RowNum = 3;

26. Write a query to display the first and the last record from the EmployeeInfo table.

To display the first record:

sql

SELECT *FROM EmployeeInfoWHERE EmpID = (SELECT MIN(EmpID) FROM EmployeeInfo);

To display the last record:

sql

SELECT *FROM EmployeeInfoWHERE EmpID = (SELECT MAX(EmpID) FROM EmployeeInfo);

27. Write a query to add email validation to your database.

sql

SELECT EmailFROM EmployeeInfoWHERE Email NOT LIKE '%@%_._%';

28. Write a query to retrieve departments that have less than 2 employees working in them.

sql

SELECT Department, COUNT(EmpID) AS EmpNoFROM EmployeeInfoGROUP BY DepartmentHAVING COUNT(EmpID) < 2;

29. Write a query to retrieve EmpPosition along with the total salaries paid for each position.

sql

SELECT EmpPosition, SUM(Salary) AS TotalSalaryFROM EmployeePositionGROUP BY EmpPosition;

30. Write a query to fetch 50% of the records from the EmployeeInfo table.

sql

SELECT *FROM (    SELECT *, ROW_NUMBER() OVER (ORDER BY EmpID) AS RowNum    FROM EmployeeInfo) AS subqueryWHERE RowNum <= (SELECT COUNT(*) FROM EmployeeInfo) / 2;

That’s it! These are the top 30 SQL query interview questions and answers that you should practice to ace your next SQL interview. Remember, practice is the key to mastering SQL queries, so keep practicing and good luck!

Top 25 SQL Interview Questions and Answers(The BEST SQL Interview Questions)

FAQ

How to write an SQL query to fetch workers names with salaries >= 50000 and 100000?

Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000. SELECT CONCAT(FIRST_NAME, ‘ ‘, LAST_NAME) As Worker_Name, Salary FROM worker WHERE WORKER_ID IN (SELECT WORKER_ID FROM worker WHERE Salary BETWEEN 50000 AND 100000);

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *