🔥 Top SQL Interview Questions with Answers
🎯 1️⃣ Find 2nd Highest Salary
📊 Table: employees
id | name | salary
1 | Rahul | 50000
2 | Priya | 70000
3 | Amit | 60000
4 | Neha | 70000
❓ Problem Statement: Find the second highest distinct salary from the employees table.
✅ Solution
SELECT MAX(salary) FROM employees WHERE salary < ( SELECT MAX(salary) FROM employees );
🎯 2️⃣ Find Nth Highest Salary
📊 Table: employees
id | name | salary
1 | A | 100
2 | B | 200
3 | C | 300
4 | D | 200
❓ Problem Statement: Write a query to find the 3rd highest salary.
✅ Solution
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER(ORDER BY salary DESC) r FROM employees ) t WHERE r = 3;
🎯 3️⃣ Find Duplicate Records
📊 Table: employees
id | name
1 | Rahul
2 | Amit
3 | Rahul
4 | Neha
❓ Problem Statement: Find all duplicate names in the employees table.
✅ Solution
SELECT name, COUNT(*) FROM employees GROUP BY name HAVING COUNT(*) > 1;
🎯 4️⃣ Customers with No Orders
📊 Table: customers
customer_id | name
1 | Rahul
2 | Priya
3 | Amit
📊 Table: orders
order_id | customer_id
101 | 1
102 | 2
❓ Problem Statement: Find customers who have not placed any orders.
✅ Solution
SELECT
c.name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL;
🎯 5️⃣ Top 3 Salaries per Department
📊 Table: employees
name | department | salary
A | IT | 100
B | IT | 200
C | IT | 150
D | HR | 120
E | HR | 180
❓ Problem Statement: Find the top 3 highest salaries in each department.
✅ Solution
SELECT * FROM ( SELECT name, department, salary, ROW_NUMBER() OVER( PARTITION BY department ORDER BY salary DESC ) r FROM employees ) t WHERE r <= 3;
🎯 6️⃣ Running Total of Sales
📊 Table: sales
date | sales
2024-01-01 | 100
2024-01-02 | 200
2024-01-03 | 300
❓ Problem Statement: Calculate the running total of sales by date.
✅ Solution
SELECT date, sales, SUM(sales) OVER(ORDER BY date) AS running_total FROM sales;
🎯 7️⃣ Employees Above Average Salary
📊 Table: employees
name | salary
A | 100
B | 200
C | 300
❓ Problem Statement: Find employees earning more than the average salary.
✅ Solution
SELECT name, salary FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees );
🎯 8️⃣ Department with Highest Total Salary
📊 Table: employees
name | department | salary
A | IT | 100
B | IT | 200
C | HR | 500
❓ Problem Statement: Find the department with the highest total salary.
✅ Solution
SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department ORDER BY total_salary DESC LIMIT 1;
🎯 9️⃣ Customers Who Placed Orders
📊 Tables: Same as Q4
❓ Problem Statement: Find customers who have placed at least one order.
✅ Solution
SELECT name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE c.customer_id = o.customer_id );
🎯 🔟 Remove Duplicate Records
📊 Table: employees
id | name
1 | Rahul
2 | Rahul
3 | Amit
❓ Problem Statement: Delete duplicate records but keep one unique record.
✅ Solution
DELETE FROM employees WHERE id NOT IN ( SELECT MIN(id) FROM employees GROUP BY name );
🚀 Pro Tip:
👉 In interviews:
First explain logic
Then write query
Then optimize
Double Tap ♥️ For More