110 SQL Query Interview Questions and Practice Exercises for Experienced and Fre
110 SQL Query Interview Questions and Practice Exercises for Experienced and Fre
What to Expect
Our SQL Query Practice Blog is divided into multiple sections, each covering different levels of
complexity. We start with simple queries for beginners, introducing essential SQL concepts and
gradually move towards more advanced scenarios for experienced users. Each section includes
practical examples, explanations, and HTML-formatted SQL queries that you can easily copy and
paste into your practice environment or projects.
Employees Table
Customers Table
Orders Table
1 3 2023-07-01 100.00 1
2 1 2023-07-05 250.00 1
3 4 2023-07-10 180.00 0
4 2 2023-07-15 300.00 1
5 5 2023-07-20 120.00 1
SQL Script to create all above three tables.
--Employees table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2),
HireDate DATE
);
--Customers table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
Address VARCHAR(200),
City VARCHAR(50),
Country VARCHAR(50)
);
INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Address, City, Country)
VALUES
(1, 'Michael', 'Brown', 'michael@example.com', '123 Main St', 'New York', 'USA'),
(2, 'Emma', 'Johnson', 'emma@example.com', '456 Elm St', 'Los Angeles', 'USA'),
(3, 'Oliver', 'Smith', 'oliver@example.com', '789 Oak St', 'Chicago', 'USA'),
(4, 'Sophia', 'Williams', 'sophia@example.com', '101 Maple Ave', 'Houston', 'USA'),
(5, 'James', 'Lee', 'james@example.com', '222 Pine St', 'San Francisco', 'USA');
--Orders table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10, 2),
IsShipped BIT
);
16. Find the nth highest salary from the Employees table.
SELECT EmployeeID, FirstName, LastName, DATEDIFF(YEAR, HireDate, GETDATE()) AS Age FROM Employ
20. Find the customers who have not placed any orders
in the last 3 months.
28. Show the employees who have the same hire date as
their manager.
SELECT E.* FROM Employees E
INNER JOIN Employees M ON E.ManagerID = M.EmployeeID
WHERE E.HireDate = M.HireDate;
31. Find the employees whose first name starts with 'J'
and last name starts with 'S'.
SELECT E.*
FROM Employees E
INNER JOIN (
SELECT Department, MAX(Salary) AS MaxSalary
FROM Employees
GROUP BY Department
) MaxSalaries ON E.Department = MaxSalaries.Department AND E.Salary = MaxSalaries.MaxSalary;
SELECT C.*
FROM Customers C
WHERE EXISTS (
SELECT *
FROM Orders O1
WHERE C.CustomerID = O1.CustomerID
AND EXISTS (
SELECT *
FROM Orders O2
WHERE O1.CustomerID = O2.CustomerID AND O1.OrderID = O2.OrderID - 1
)
);
SELECT C.*
FROM Customers C
WHERE C.CustomerID IN (
SELECT TOP 1 WITH TIES CustomerID
FROM Orders O
GROUP BY CustomerID, Country
ORDER BY COUNT(*) DESC
);
SELECT O.*
FROM Orders O
WHERE O.OrderID IN (
SELECT TOP 1 WITH TIES OrderID
FROM Orders
WHERE YEAR(OrderDate) = YEAR(O.OrderDate)
ORDER BY TotalAmount DESC
);
SELECT C.*
FROM Customers C
WHERE EXISTS (
SELECT * FROM Orders O1
WHERE C.CustomerID = O1.CustomerID
AND NOT EXISTS (
SELECT * FROM Orders O2
WHERE O1.CustomerID = O2.CustomerID
AND DATEPART(WEEKDAY, O2.OrderDate) NOT BETWEEN 2 AND 6
)
)
AND C.CustomerID IN (
SELECT TOP 1 WITH TIES CustomerID
FROM Orders O
GROUP BY CustomerID, City
ORDER BY SUM(TotalAmount) DESC
);
SELECT C.*
FROM Customers C
WHERE EXISTS (
SELECT * FROM Orders O1
WHERE C.CustomerID = O1.CustomerID
AND NOT EXISTS (
SELECT * FROM Orders O2
WHERE O1.CustomerID = O2.CustomerID
AND DATEPART(WEEKDAY, O2.OrderDate) NOT BETWEEN 2 AND 6
)
)
AND C.CustomerID IN (
SELECT TOP 1 WITH TIES CustomerID
FROM Orders O
GROUP BY CustomerID, Country
ORDER BY SUM(TotalAmount) DESC
);
64. Find the employees who have more than one
subordinate and have been hired before their manager,
and their age is a prime number.
SELECT E.*
FROM Employees E
INNER JOIN (
SELECT ManagerID, COUNT(*) AS SubordinateCount
FROM Employees
GROUP BY ManagerID
HAVING COUNT(*) > 1
) Subordinates ON E.EmployeeID = Subordinates.ManagerID
WHERE E.HireDate < (
SELECT HireDate FROM Employees WHERE EmployeeID = E.ManagerID
)
AND DATEDIFF(YEAR, E.HireDate, GETDATE()) IN (
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
);
66. Show the customers who have not placed any orders
in the last 6 months and have the highest total order
amount.
SELECT E.*
FROM Employees E
WHERE NOT EXISTS (
SELECT Department FROM Departments
WHERE NOT EXISTS (
SELECT * FROM Employees
WHERE EmployeeID = E.EmployeeID AND Department = Departments.Department
)
);
73. Show the orders that have the same total amount as
the average total amount of orders for each year.
SELECT O.*
FROM Orders O
WHERE O.TotalAmount IN (
SELECT AVG(TotalAmount) AS AvgAmount
FROM Orders
WHERE YEAR(OrderDate) = YEAR(O.OrderDate)
GROUP BY YEAR(OrderDate)
);
74. Retrieve the employees who have been assigned to
the maximum number of departments.
SELECT E.*
FROM Employees E
WHERE EmployeeID IN (
SELECT TOP 1 WITH TIES EmployeeID
FROM Employees
GROUP BY EmployeeID
ORDER BY COUNT(DISTINCT Department) DESC
);
75. Find the customers who have placed orders with the
same total amount as another customer from a
different country.
SELECT C.*
FROM Customers C
WHERE EXISTS (
SELECT * FROM Orders O1
WHERE C.CustomerID = O1.CustomerID
AND EXISTS (
SELECT * FROM Orders O2
WHERE O1.TotalAmount = O2.TotalAmount
AND O1.CustomerID <> O2.CustomerID
AND C.Country <> (
SELECT Country FROM Customers WHERE CustomerID = O2.CustomerID
)
)
);
SELECT C.*
FROM Customers C
WHERE EXISTS (
SELECT *
FROM Orders O1
WHERE C.CustomerID = O1.CustomerID
AND EXISTS (
SELECT *
FROM Orders O2
WHERE O1.CustomerID = O2.CustomerID AND O1.OrderID = O2.OrderID - 1
)
)
AND C.CustomerID IN (
SELECT TOP 1 WITH TIES CustomerID
FROM Orders O
GROUP BY CustomerID, Country
ORDER BY SUM(TotalAmount) DESC
);
SELECT E.*
FROM Employees E
WHERE NOT EXISTS (
SELECT Department FROM Departments
WHERE NOT EXISTS (
SELECT * FROM Employees
WHERE EmployeeID = E.EmployeeID AND Department = Departments.Department
)
)
AND DATEDIFF(YEAR, E.HireDate, GETDATE()) % 2 <> 0;
SELECT C.*
FROM Customers C
INNER JOIN Orders O ON C.CustomerID = O.CustomerID
WHERE DAY(O.OrderDate) = DAY(CAST(DATEADD(YEAR, DATEDIFF(YEAR, BirthDate, GETDATE()), BirthDat
AND MONTH(O.OrderDate) = MONTH(CAST(DATEADD(YEAR, DATEDIFF(YEAR, BirthDate, GETDATE()), BirthD
SELECT E.*
FROM Employees E
WHERE NOT EXISTS (
SELECT * FROM Departments D
WHERE NOT EXISTS (
SELECT * FROM Employees
WHERE EmployeeID = E.EmployeeID AND Department = D.DepartmentID
)
);
96. Retrieve the employees who have the same first
name as another employee and their salaries are within
$1000 of each other.
SELECT E.*
FROM Employees E
WHERE EXISTS (
SELECT * FROM Employees E2
WHERE E.EmployeeID <> E2.EmployeeID
AND E.FirstName = E2.FirstName
AND ABS(E.Salary - E2.Salary) <= 1000
);
97. Find the customers who have placed orders with the
same total amount as other customers and have the
same city as those customers.
SELECT C.*
FROM Customers C
WHERE EXISTS (
SELECT * FROM Orders O1
WHERE C.CustomerID = O1.CustomerID
AND EXISTS (
SELECT * FROM Orders O2
WHERE O1.TotalAmount = O2.TotalAmount
AND O1.CustomerID <> O2.CustomerID
AND C.City = (
SELECT City FROM Customers WHERE CustomerID = O2.CustomerID
)
)
);
98. Show the employees who have the same hire year as
the highest-earning employee in each department.
SELECT E.*
FROM Employees E
WHERE E.HireDate IN (
SELECT MAX(HireDate) FROM Employees WHERE Department = E.Department
);
SELECT C.*
FROM Customers C
INNER JOIN Orders O ON C.CustomerID = O.CustomerID
WHERE CAST(O.OrderDate AS DATE) = CAST(DATEADD(YEAR, DATEDIFF(YEAR, BirthDate, GETDATE()), Bir
SELECT E.*
FROM Employees E
WHERE E.Department IS NOT NULL
AND E.EmployeeID NOT IN (
SELECT EmployeeID FROM SalaryHistory WHERE SalaryDate >= DATEADD(YEAR, -2, GETDATE())
);
SELECT C.*
FROM Customers C
INNER JOIN Orders O ON C.CustomerID = O.CustomerID
WHERE MONTH(O.OrderDate) = 2 AND DAY(O.OrderDate) = 29;
SELECT E.*
FROM Employees E
WHERE E.EmployeeID NOT IN (
SELECT EmployeeID FROM SalaryHistory
WHERE YEAR(SalaryDate) < YEAR(E.HireDate)
)
AND E.EmployeeID NOT IN (
SELECT EmployeeID FROM SalaryHistory
GROUP BY EmployeeID
HAVING COUNT(DISTINCT YEAR(SalaryDate)) < DATEDIFF(YEAR, E.HireDate, GETDATE()) + 1
);
SELECT C.*
FROM Customers C
INNER JOIN Orders O ON C.CustomerID = O.CustomerID
WHERE DATEPART(WEEKDAY, O.OrderDate) = DATEPART(WEEKDAY, (SELECT MIN(OrderDate) FROM Orders));
Conclusion
In conclusion, SQL queries are essential tools for working with relational databases. They allow us
to extract, manipulate, and transform data to gain valuable insights and answer specific
questions. In this practice session, we covered a wide range of SQL queries, starting from basic
queries and gradually increasing the complexity for both beginners and experienced users.
For Beginners
For beginners, we focused on fundamental SQL concepts such as SELECT, FROM, WHERE, GROUP BY,
HAVING, ORDER BY, and JOIN clauses. We practiced writing queries to retrieve, filter, and
aggregate data from different tables.
Advancing Complexity
As the practice advanced, we delved into more complex queries involving subqueries, common
table expressions (CTEs), window functions, and set operations. We also explored scenarios like
handling NULL values, using string functions, and working with date and time data.
Continued Learning
The practice session also provided HTML-formatted SQL queries, making it convenient for users to
copy and paste them into their blogs or practice environments. It's important to note that while
these examples cover a wide array of SQL query types, the field of SQL is vast, and there is always
Mastering Azure Synapse Analytics: Top 100 Interview Questions with Detailed Answers
Top 100 Azure Synapse Analytics Interview Questions with Detailed Answers Super…