1. Find the second highest salary:
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
2. Count employees in each department:
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
3. Fetch duplicate emails:
SELECT email, COUNT() FROM users GROUP BY email HAVING COUNT() > 1;
4. Join orders with customer names:
SELECT c.name, o.order_date
FROM customers c
JOIN orders o ON c.id = o.customer_id;
5. Get top 3 highest salaries:
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 3;
6. Retrieve latest 5 logins:
SELECT * FROM logins
ORDER BY login_time DESC
LIMIT 5;
7. Employees with no manager:
SELECT name
FROM employees
WHERE manager_id IS NULL;
8. Search names starting with ‘S’:
SELECT * FROM employees
WHERE name LIKE 'S%';
9. Total sales per month:
SELECT MONTH(order_date) AS month, SUM(amount)
FROM sales
GROUP BY MONTH(order_date);
10. Delete inactive users:
DELETE FROM users
WHERE last_active < '2023-01-01';
Tip: Master subqueries, joins, groupings & filters – they show up in nearly every interview!