Most Frequently Asked SQL Interview Questions With Their Answers
Most Frequently Asked SQL Interview Questions With Their Answers
1. Write a SQL query to find the top 5 customers with the highest total purchase amount. Assume
you have two tables: Customers (CustomerID, Name) and Orders (OrderID, CustomerID, Amount).
, SUM(o.Amount) AS TotalPurchase
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c.Name
2. Write a query to find the nth highest salary from a table Employees with columns EmployeeID,
Name, and Salary.
Replace `n` with the desired rank (e.g., 2 for the second highest).
3. Given a table Sales with columns SaleID, ProductID, SaleDate, and Quantity, write a query to find
the total quantity sold for each product per month.
4. Write a SQL query to find all employees who have more than one manager. Assume you have a
table Employees (EmployeeID, Name, ManagerID).
5. Given a table Orders with columns OrderID, CustomerID, OrderDate, and a table OrderDetails with
columns OrderID, ProductID, Quantity, write a query to find the top 3 products with the highest sales
quantity.
SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM OrderDetails
GROUP BY ProductID
ORDER BY TotalQuantity DESC
LIMIT 3;
6. Write a SQL query to find the second most recent order date for each customer from a table
Orders (OrderID, CustomerID, OrderDate).
7. Given a table Employees with columns EmployeeID, Name, DepartmentID, Salary, write a query to
find the highest paid employee in each department.
8. Given a table Products with columns ProductID, Name, Price, and a table Sales with columns
SaleID, ProductID, Quantity, write a query to find the product with the highest revenue.