Top 25 SQL Query Interview Questions for 2022

Many of you must have to face the SQL query questions in the technical interviews. We have listed most frequently asked SQL Query Interview Questions in this post which will help for your interview preparation

This SQL Query Interview Questions and Answers post is designed for both freshers as well as experienced software professionals.

SQL Query Interview Questions


SQL Query Interview Questions and Answers

The following two sample tables are used for the reference. These SQL Query Interview Questions are based on these tables.

Employee Table

EmpIdFirstNameLastNameGenderDateOfBirthCityDateOfJoining
1StephenMcDonaldM1969-12-22Austin1989-02-22
2JulieStewartF1974-09-23New York2004-07-26
3TonyLewisM1976-10-12Atlanta2016-09-23
4RussPorterM1972-02-17Denver2000-01-01
5AshleyBensanF1990-11-12Houston2010-12-12
6AmendaVentraneF1978-07-22Dallas2017-03-13
7MariWilsonF1975-12-09New York2019-04-18
8RussStawartM1969-02-23Houston2009-11-23
9MikeGalenosM1967-03-09Las Vegas2011-09-03
10NicholasCirelloM1998-01-23New York2019-11-01

EmployeeDetails Table

EmpIdFirstNameLastNameGenderDateOfBirthCityDateOfJoining
1StephenMcDonaldM1969-12-22Austin1989-02-22
2JulieStewartF1974-09-23New York2004-07-26
3TonyLewisM1976-10-12Atlanta2016-09-23
4RussPorterM1972-02-17Denver2000-01-01
5AshleyBensanF1990-11-12Houston2010-12-12
6AmendaVentraneF1978-07-22Dallas2017-03-13
7MariWilsonF1975-12-09New York2019-04-18
8RussStawartM1969-02-23Houston2009-11-23
9MikeGalenosM1967-03-09Las Vegas2011-09-03
10NicholasCirelloM1998-01-23New York2019-11-01

Let us get started!

Basic SQL Query Interview Questions and Answers

Que 1. Write a query to get the current date in SQL Server.

SELECT GETDATE() 


Que 2. Write a SQL query to get all the employee orders by Joining Date in descending order?

SELECT * FROM Employee
ORDER BY DateOfJoining DESC 


Que 3. Write a SQL query to find all the employees who are from New York

SELECT * 
FROM Employee
WHERE City = 'New York' 


Que 4. Write a SQL query to retrieve the first three characters of FirstName of the employee.

SELECT SUBSTRING(FirstName, 1, 3) 
FROM Employee 


Que 5. Write a SQL query to fetch the first name of the employee in the upper case and last name in lower case.

SELECT UPPER(FirstName), LOWER(LastName)
FROM Employee 


Que 6. Write a SQL query to find all the male employees whose first name starts with ‘R’?

SELECT * 
FROM Employee 
WHERE FirstName LIKE 'R%' AND Gender = 'M' 


Que 7. Write a SQL query to find all the female employees whose first name ends with ‘y’?

SELECT * 
FROM Employee 
WHERE FirstName LIKE '%y' AND Gender = 'F' 


Que 8. Write a query to fetch all the employees who hold the Manager designation.

SELECT E.FirstName, E.LastName, D.Designation 
FROM Employee E 
INNER JOIN EmployeeDetails D 
ON E.EmpId = D.EmpID AND D.Designation = 'Manager' 


Que 9. Write a SQL query that fetches the unique records of the department.

SELECT DISTINCT Department
FROM EmployeeDetails 


Que 10. Write a SQL query to fetch the FirstName and LastName from the Employee table as a single column FullName.

SELECT CONCAT(FirstName, ' ', LastName) AS FullName 
FROM Employee 


Que 11. Write a SQL query to get all the employee orders by Date of Birth in ascending order?

SELECT *
FROM Employee
ORDER BY DateOfBirth ASC 


Que 12. Write a SQL query to fetch the count of employees works in the department 'Engineering'.

SELECT COUNT(*) 
FROM EmployeeDetails
WHERE Department = 'Engineering' 


Que 13. Write a SQL query to fetch all the employees with the same salary.

SELECT E.EmpId, CONCAT(E.FirstName, ' ', E.LastName) AS FullName, ED.Salary, ED.Department, ED.Designation
FROM Employee E
INNER JOIN EmployeeDetails ED
ON E.EmpId = ED.EmpId
WHERE ED.Salary IN (SELECT Salary FROM EmployeeDetails
GROUP BY Salary
HAVING COUNT (1)>1) 


Que 14. Write a SQL query to fetch the employee department after removing the white spaces from the left side.

SELECT LTRIM(Department) 
FROM EmployeeDetails 


Que 15. Write a SQL query to fetch the employee department after removing the white spaces from the right side.

SELECT RTRIM(Department) 
FROM EmployeeDetails 


Advanced SQL Query Interview Questions and Answers

Que 16. Write an SQL query to find the maximum, minimum, and average salary of the employees.

SELECT MAX(Salary), 
	Min(Salary), 
	AVG(Salary) 
FROM EmployeeDetails 


Que 17. Write a SQL query to fetch the third highest salary of an employee?

This is one of the very popular SQL Query Interview Questions
SELECT TOP 1 Salary
FROM(
		SELECT TOP 3 Salary
		FROM EmployeeDetails
		ORDER BY salary DESC) AS emp
ORDER BY Salary ASC 


Que 18. Write a SQL query to fetch two minimum salaries.

SELECT DISTINCT Salary 
FROM EmployeeDetails ED 
WHERE 2 >= (
				SELECT COUNT(DISTINCT Salary) FROM EmployeeDetails EDD 
				WHERE ED.Salary >= EDD.Salary
			) 
ORDER BY ED.Salary DESC 


Que 19. Write a SQL query to fetch two maximum salaries.

SELECT DISTINCT Salary 
FROM EmployeeDetails ED 
WHERE 2 >= (
				SELECT COUNT(DISTINCT Salary) FROM EmployeeDetails EDD 
				WHERE ED.Salary <= EDD.Salary
			) 
ORDER BY ED.Salary DESC 


Que 20. Write a SQL query to retrieve duplicate records from a table.

SELECT EmpID, Department, COUNT(*) 
FROM EmployeeDetails 
GROUP BY EmpID, Department 
HAVING COUNT(*) > 1 


Que 21. Write a SQL query to fetch only odd rows from the table.

SELECT E.EmpId, E.Department, E.Salary
FROM (
		SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber
		FROM EmployeeDetails
	 ) E
WHERE E.RowNumber % 2 = 1 


Que 22. Write an SQL query to fetch only even rows from the table.

SELECT E.EmpId, E.Department, E.Salary
FROM (
		SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber
		FROM EmployeeDetails
	 ) E
WHERE E.RowNumber % 2 = 0 


Que 23. Write a SQL query to fetch the last three records from a table.

SELECT TOP 3 *
FROM Employee  
ORDER BY EmpId DESC 


Que 24: Write a SQL Query to fetch Max Salary from each department.

SELECT Department, MAX(Salary) AS Salary
GROUP BY Department
FROM EmployeeDetails 


Que 25. Write a SQL query which contains JOIN, WHERE, GROUP BY, HAVING, and ORDER By Clauses?

SELECT COUNT(E.City) AS 'Total Employees', E.City
FROM Employee E
INNER JOIN EmployeeDetails ED
ON E.EmpId = ED.EmpId
WHERE ED.Department = 'Engineering'
GROUP BY E.City
HAVING COUNT(E.City) > 2 

This SQL query fetches the records of the engineering departments employees whose count is more than two in a particular city

Summary

These are the frequently asked SQL Query Interview Questions and Answers based on our interview experience. This article will be helpful for your SQL developer interview preparation.