Structured Query Language , is the primary instrument used when working with relational databases, so everyone who is connected with databases should know it. Whether you are new to SQL or just want to polish up your knowledge, this article guides you through 50 specifically designed SQL Query Interview Questions from entry level to advanced level.
To better understand and execute the SQL queries, let’s begin by examining the sample data used in this article. The following tables will serve as the basis for all the queries and examples.
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID |
---|---|---|---|---|---|---|
1 | Alice | Johnson | 101 | 60000 | 2018-01-15 | 3 |
2 | Bob | Smith | 102 | 75000 | 2017-05-20 | 3 |
3 | Charlie | Brown | 101 | 90000 | 2015-09-30 | NULL |
4 | David | Williams | 103 | 55000 | 2019-07-11 | 3 |
5 | Eva | Davis | 102 | 65000 | 2020-03-25 | 2 |
OrderID | EmployeeID | ProductID | Quantity | OrderDate |
---|---|---|---|---|
1001 | 1 | 201 | 10 | 2022-01-15 |
1002 | 2 | 202 | 5 | 2022-01-16 |
1003 | 3 | 203 | 20 | 2022-01-17 |
1004 | 4 | 202 | 15 | 2022-01-18 |
1005 | 5 | 204 | 25 | 2022-01-19 |
ProductID | ProductName | Price | Category |
---|---|---|---|
201 | Laptop | 1200 | Electronics |
202 | Smartphone | 800 | Electronics |
203 | Office Chair | 150 | Furniture |
204 | Desk | 300 | Furniture |
205 | Monitor | 200 | Electronics |
In this section, let us provide a guideline of basic SQL queries that students who are new to SQL can easily understand. These basic queries can be used as the foundation to achieving a comfort level with the most important aspects of SQL, including selection of data, forms of data filtering and rudimentary computations.
Answer:
SELECT * FROM Employees;
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
1 | Alice | Johnson | 10 | 160000 | 2018-01-15 | 32
2 | Bob | Smith | 10 | 275000 | 2017-05-20 | 33
3 | Charlie | Brown | 10 | 190000 | 2015-09-30 | NULL
4 | David | Williams | 10 | 355000 | 2019-07-11 | 35
5 | Eva | Davis | 10 | 265000 | 2020-03-25 | 32
Answer:
SELECT FirstName, LastName FROM Employees;
FirstName | LastName
--------------------
Alice | Johnson
Bob | Smith
Charlie | Brown
David | Williams
Eva | Davis
Answer:
SELECT DISTINCT DepartmentID FROM Employees;
DepartmentID
-------------
10
Answer:
SELECT * FROM Employees WHERE Salary > 60000;
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
1 | Alice | Johnson | 10 | 160000 | 2018-01-15 | 32
2 | Bob | Smith | 10 | 275000 | 2017-05-20 | 33
3 | Charlie | Brown | 10 | 190000 | 2015-09-30 | NULL
4 | David | Williams | 10 | 355000 | 2019-07-11 | 35
5 | Eva | Davis | 10 | 265000 | 2020-03-25 | 32
Answer:
SELECT * FROM Orders WHERE OrderDate >= '2022-01-17';
OrderID | EmployeeID | ProductID | Quantity | OrderDate
----------------------------------------------------------
1022 | 2 | 1 | 2 | 2022-01-16
1023 | 3 | 3 | 3 | 2022-01-17
1024 | 4 | 2 | 5 | 2022-01-18
1025 | 5 | 4 | 5 | 2022-01-19
Answer:
SELECT * FROM Products WHERE Price < 300;
ProductID | ProductName | Price | Category
--------------------------------------------
203 | Office Chair | 150 | Furniture
204 | Desk | 300 | Furniture
205 | Monitor | 200 | Electronics
Answer:
SELECT COUNT(*) AS TotalOrders FROM Orders;
TotalOrders
------------
5
Answer:
SELECT * FROM Products WHERE ProductName = 'Laptop';
ProductID | ProductName | Price | Category
--------------------------------------------
201 | Laptop | 1200 | Electronics
Answer:
SELECT * FROM Employees ORDER BY HireDate ASC;
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
3 | Charlie | Brown | 10 | 190000 | 2015-09-30 | NULL
2 | Bob | Smith | 10 | 275000 | 2017-05-20 | 33
1 | Alice | Johnson | 10 | 160000 | 2018-01-15 | 32
4 | David | Williams | 10 | 355000 | 2019-07-11 | 35
5 | Eva | Davis | 10 | 265000 | 2020-03-25 | 32
Answer:
SELECT MAX(Price) AS MaxPrice FROM Products WHERE Category = 'Electronics';
MaxPrice
--------
1200
The following section of the article deals with the intermediate level in Learning SQL by presenting more comprehensive queries. You will continue with joining tables, learn how to use functions to filter data as well as complex operations to solve real-world operations better.
Answer:
SELECT e.FirstName, e.LastName, o.OrderID, o.OrderDate
FROM Employees e
JOIN Orders o ON e.EmployeeID = o.EmployeeID;
FirstName | LastName | OrderID | OrderDate
-------------------------------------------
Alice | Johnson | 1022 | 2022-01-16
Bob | Smith | 1023 | 2022-01-17
Charlie | Brown | 1024 | 2022-01-18
David | Williams | 1025 | 2022-01-19
Answer:
SELECT DepartmentID, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY DepartmentID;
DepartmentID | TotalSalary
-------------------------
10 | 1355000
Answer:
SELECT * FROM Employees WHERE ManagerID IS NULL;
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
3 | Charlie | Brown | 10 | 190000 | 2015-09-30 | NULL
Answer:
SELECT Category, AVG(Price) AS AvgPrice
FROM Products
GROUP BY Category;
Category | AvgPrice
-----------------------
Electronics | 800
Furniture | 216.67
Answer:
SELECT * FROM Employees
ORDER BY Salary DESC
LIMIT 3;
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
4 | David | Williams | 10 | 355000 | 2019-07-11 | 35
2 | Bob | Smith | 10 | 275000 | 2017-05-20 | 33
5 | Eva | Davis | 10 | 265000 | 2020-03-25 | 32
Answer:
SELECT o.OrderID, o.Quantity, p.ProductName, p.Price
FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID;
OrderID | Quantity | ProductName | Price
-------------------------------------------
1022 | 2 | Laptop | 1200
1023 | 3 | Office Chair | 150
1024 | 5 | Smartphone | 800
1025 | 5 | Desk | 300
Answer:
SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM Orders
GROUP BY ProductID;
ProductID | TotalQuantity
--------------------------
1 | 2
2 | 8
3 | 3
4 | 5
Answer:
UPDATE Products
SET Price = Price * 1.10
WHERE Category = 'Furniture';
Answer:
DELETE FROM Orders WHERE OrderDate < '2022-01-17';
Answer:
SELECT * FROM Employees WHERE FirstName LIKE 'A%';
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
1 | Alice | Johnson | 10 | 160000 | 2018-01-15 | 32
Answer:
SELECT YEAR(HireDate) AS HireYear, COUNT(*) AS EmployeesHired
FROM Employees
GROUP BY YEAR(HireDate);
HireYear | EmployeesHired
-------------------------
2015 | 1
2017 | 1
2018 | 1
2019 | 1
2020 | 1
Answer:
SELECT * FROM Employees
WHERE Salary > (SELECT AVG(Salary) FROM Employees);
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
2 | Bob | Smith | 10 | 275000 | 2017-05-20 | 33
4 | David | Williams | 10 | 355000 | 2019-07-11 | 35
5 | Eva | Davis | 10 | 265000 | 2020-03-25 | 32
Answer:
SELECT p.ProductName, SUM(o.Quantity) AS TotalQuantity
FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
GROUP BY p.ProductName
ORDER BY TotalQuantity DESC
LIMIT 3;
ProductName | TotalQuantity
----------------------------
Smartphone | 8
Desk | 5
Office Chair| 3
Answer:
SELECT * FROM Employees
WHERE EmployeeID NOT IN (SELECT DISTINCT EmployeeID FROM Orders);
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
3 | Charlie | Brown | 10 | 190000 | 2015-09-30 | NULL
Answer:
SELECT * FROM Employees
ORDER BY HireDate DESC
LIMIT 1;
EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate | ManagerID
----------------------------------------------------------------------------
5 | Eva | Davis | 10 | 265000 | 2020-03-25 | 32
Answer:
SELECT e.EmployeeID, e.FirstName, COUNT(o.OrderID) AS TotalOrders
FROM Employees e
LEFT JOIN Orders o ON e.EmployeeID = o.EmployeeID
GROUP BY e.EmployeeID, e.FirstName;
EmployeeID | FirstName | TotalOrders |
---|---|---|
1 | Alice | 2 |
2 | Bob | 2 |
3 | Charlie | 1 |
4 | David | 1 |
5 | Eva | 0 |
Answer:
SELECT p.ProductName, SUM(o.Quantity * p.Price) AS TotalSales
FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
GROUP BY p.ProductName
HAVING TotalSales > 10000;
ProductName | TotalSales |
---|---|
Laptop | 24000 |
Answer:
SELECT e.FirstName AS EmployeeName, m.FirstName AS ManagerName
FROM Employees e
JOIN Employees m ON e.ManagerID = m.EmployeeID
WHERE YEAR(e.HireDate) = YEAR(m.HireDate);
EmployeeName | ManagerName |
---|---|
Alice | Bob |
Answer:
SELECT DepartmentID, FirstName, LastName, Salary
FROM Employees
WHERE (DepartmentID, Salary) IN (
SELECT DepartmentID, MAX(Salary)
FROM Employees
GROUP BY DepartmentID
);
DepartmentID | FirstName | LastName | Salary |
---|---|---|---|
1 | Alice | Johnson | 160000 |
2 | Bob | Smith | 75000 |
3 | David | Williams | 55000 |
Answer:
SELECT e.FirstName, e.LastName, SUM(o.Quantity * p.Price) AS TotalRevenue
FROM Employees e
JOIN Orders o ON e.EmployeeID = o.EmployeeID
JOIN Products p ON o.ProductID = p.ProductID
GROUP BY e.EmployeeID, e.FirstName, e.LastName;
FirstName | LastName | TotalRevenue |
---|---|---|
Alice | Johnson | 32000 |
Bob | Smith | 63000 |
Charlie | Brown | 45000 |
David | Williams | 30000 |
Eva | Davis | 0 |
In the advanced level, we deal with complex synthesis of SQL query statements. This section is devoted to the description of more complex operations like ranking, window functions, basic subqueries, and optimization methods to allow you overcoming complicated tasks in data analysis.
Answer:
SELECT e.FirstName AS EmployeeName, m.FirstName AS ManagerName
FROM Employees e
JOIN Employees m ON e.ManagerID = m.EmployeeID
WHERE e.Salary > m.Salary;
EmployeeName | ManagerName |
---|---|
Alice | Bob |
Answer:
SELECT MAX(Salary) AS SecondHighestSalary
FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);
SecondHighestSalary
75000
Answer:
SELECT * FROM Departments
WHERE DepartmentID NOT IN (SELECT DISTINCT DepartmentID FROM Employees);
DepartmentID | DepartmentName |
---|---|
4 | Marketing |
Answer:
CREATE VIEW EmployeeDepartmentView AS
SELECT e.FirstName, e.LastName, d.DepartmentName
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID;
FirstName | LastName | DepartmentName |
---|---|---|
Alice | Johnson | IT |
Bob | Smith | Sales |
Charlie | Brown | IT |
David | Williams | HR |
Eva | Davis | Sales |
Answer:
SELECT e.FirstName, e.LastName
FROM Employees e
JOIN Orders o ON e.EmployeeID = o.EmployeeID
GROUP BY e.EmployeeID, e.FirstName, e.LastName
HAVING COUNT(o.OrderID) > 10;
FirstName | LastName |
---|---|
Alice | Johnson |
Bob | Smith |
Answer:
SELECT EmployeeID, FirstName, DepartmentID, Salary,
RANK() OVER (PARTITION BY DepartmentID ORDER BY Salary DESC) AS Rank
FROM Employees;
EmployeeID | FirstName | DepartmentID | Salary | Rank |
---|---|---|---|---|
1 | Alice | 1 | 160000 | 1 |
3 | Charlie | 1 | 190000 | 2 |
2 | Bob | 2 | 75000 | 1 |
4 | David | 3 | 55000 | 1 |
5 | Eva | 2 | 65000 | 2 |
Answer:
SELECT ProductID, ProductName,
SUM(SUM(Quantity * Price)) OVER (ORDER BY ProductID) AS CumulativeSales
FROM Products p
JOIN Orders o ON p.ProductID = o.ProductID
GROUP BY ProductID, ProductName;
ProductID | ProductName | CumulativeSales |
---|---|---|
201 | Laptop | 24000 |
202 | Smartphone | 32000 |
203 | Office Chair | 1500 |
204 | Desk | 3000 |
205 | Monitor | 1500 |
Answer:
SELECT DepartmentID, SUM(Salary) AS TotalExpenditure
FROM Employees
GROUP BY DepartmentID
ORDER BY TotalExpenditure DESC
LIMIT 1;
DepartmentID | TotalExpenditure |
---|---|
1 | 450000 |
Answer:
SELECT p.ProductName,
(SUM(o.Quantity * p.Price) * 100.0 /
(SELECT SUM(Quantity * Price) FROM Orders o JOIN Products p ON o.ProductID = p.ProductID)) AS ContributionPercentage
FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
GROUP BY p.ProductName;
ProductName | ContributionPercentage |
---|---|
Laptop | 48.00 |
Smartphone | 32.00 |
Office Chair | 4.00 |
Desk | 8.00 |
Monitor | 8.00 |
Answer:
SELECT *
FROM Employees e1
WHERE ManagerID IS NOT NULL
AND Salary > 70000
AND ManagerID IN (
SELECT ManagerID FROM Employees e2 WHERE e1.ManagerID = e2.ManagerID
);
EmployeeID | FirstName | LastName | Salary | ManagerID |
---|---|---|---|---|
1 | Alice | Johnson | 160000 | 32 |
2 | Bob | Smith | 75000 | 32 |
Answer:
SELECT EmployeeID, ProductID, OrderDate, COUNT(*) AS DuplicateCount
FROM Orders
GROUP BY EmployeeID, ProductID, OrderDate
HAVING COUNT(*) > 1;
EmployeeID | ProductID | OrderDate | DuplicateCount |
---|---|---|---|
1 | 201 | 2022-01-15 | 2 |
Answer:
SELECT OrderDate, COUNT(DISTINCT EmployeeID) AS EmployeeCount
FROM Orders
GROUP BY OrderDate
HAVING EmployeeCount > 1;
OrderDate | EmployeeCount |
---|---|
2022-01-15 | 2 |
2022-01-16 | 2 |
2022-01-17 | 1 |
Answer:
DELIMITER $$
CREATE PROCEDURE UpdatePriceByCategory(IN category_name VARCHAR(50), IN price_factor DECIMAL(5, 2))
BEGIN
UPDATE Products
SET Price = Price * price_factor
WHERE Category = category_name;
END$$
DELIMITER ;
Answer:
SELECT EmployeeID, OrderID, OrderDate,
LAG(OrderDate) OVER (PARTITION BY EmployeeID ORDER BY OrderDate) AS PreviousOrderDate,
LEAD(OrderDate) OVER (PARTITION BY EmployeeID ORDER BY OrderDate) AS NextOrderDate
FROM Orders;
EmployeeID | OrderID | OrderDate | PreviousOrderDate | NextOrderDate |
---|---|---|---|---|
1 | 1 | 2022-01-15 | NULL | 2022-01-16 |
2 | 2 | 2022-01-16 | 2022-01-15 | 2022-01-17 |
3 | 3 | 2022-01-17 | NULL | NULL |
Answer:
SELECT * FROM Products
WHERE ProductID NOT IN (SELECT DISTINCT ProductID FROM Orders);
ProductID | ProductName |
---|---|
204 | Desk |
205 | Monitor |
Answer:
SELECT e.FirstName, e.LastName, SUM(o.Quantity) AS TotalQuantity
FROM Employees e
JOIN Orders o ON e.EmployeeID = o.EmployeeID
GROUP BY e.EmployeeID, e.FirstName, e.LastName
HAVING TotalQuantity BETWEEN 50 AND 100;
FirstName | LastName | TotalQuantity |
---|---|---|
Bob | Smith | 60 |
Answer:
SELECT ProductID, MAX(Quantity) AS SecondHighestQuantity
FROM Orders
WHERE Quantity < (SELECT MAX(Quantity) FROM Orders WHERE Orders.ProductID = ProductID)
GROUP BY ProductID;
ProductID | SecondHighestQuantity |
---|---|
201 | 20 |
202 | 30 |
203 | 10 |
Answer:
SELECT EmployeeID, MIN(Quantity) AS MinQuantity, MAX(Quantity) AS MaxQuantity
FROM Orders
GROUP BY EmployeeID;
EmployeeID | MinQuantity | MaxQuantity |
---|---|---|
1 | 10 | 20 |
2 | 20 | 40 |
3 | 10 | 10 |
Answer:
SELECT EmployeeID, FirstName, Salary,
NTILE(4) OVER (ORDER BY Salary) AS SalaryQuartile
FROM Employees;
EmployeeID | FirstName | Salary | SalaryQuartile |
---|---|---|---|
1 | Alice | 160000 | 4 |
2 | Bob | 75000 | 3 |
3 | Charlie | 190000 | 4 |
4 | David | 55000 | 2 |
5 | Eva | 65000 | 2 |
Answer:
CREATE TEMPORARY TABLE HighRevenueOrders AS
SELECT o.OrderID, o.Quantity, p.Price, (o.Quantity * p.Price) AS Revenue
FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
WHERE (o.Quantity * p.Price) > 5000;
OrderID | Quantity | Price | Revenue |
---|---|---|---|
1 | 10 | 1200 | 12000 |
2 | 25 | 800 | 20000 |
Mastering SQL Query Interview Questions provides a solid foundation for efficient data management and analysis. By practicing these SQL Query Interview Questions, you enhance your ability to work with real-world databases, making it easier to retrieve, manipulate, and interpret data effectively. Whether you’re just starting or refining your skills, SQL remains an essential tool for any data professional, and understanding its diverse capabilities will unlock countless opportunities for problem-solving and insight generation.