Top 50 SQL Questions
🔥 1. Basic → Intermediate (Warm-up but important)
1. Find duplicate records in a table
2. Delete duplicate records (keep latest)
3. Find second highest salary
4. Find Nth highest salary
5. Get employees earning more than their manager
6. Count employees in each department
7. Departments with more than 5 employees
8. Get employees who joined in last 30 days
9. Find records with NULL values in specific columns
10. Replace NULL values with default
⚡ 2. Joins & Relationships (Very common)
11. Find customers who never placed an order
12. Find orders without matching customers
13. Self join to find employee-manager hierarchy
14. Find mutual friends (self join problem)
15. Cross join to generate combinations
16. Find missing IDs in a sequence
17. Find unmatched records between two tables
18. Anti-join using NOT EXISTS
19. Compare two tables and find differences
20. Join 3+ tables and aggregate results
🚀 3. Window Functions (VERY IMPORTANT 🔥)
21. Assign row numbers to each partition
22. Find top 3 salaries per department
23. Find latest record per customer
24. Calculate running total
25. Moving average (last 3 days)
26. Find gaps in sequence (missing dates)
27. Rank employees by salary
28. Difference between consecutive rows
29. Find duplicate records using window functions
30. Get first and last record per group
🧠 4. Aggregation & Grouping (Scenario-heavy)
31. Total sales per region per month
32. Find highest selling product per category
33. Conditional aggregation (CASE WHEN)
34. Pivot rows into columns
35. Unpivot columns into rows
36. Count distinct values per group
37. Find percentage contribution per group
38. Find median salary
39. Find departments with highest avg salary
40. Group consecutive records
🔥 5. Subqueries & CTEs
41. Use CTE to find hierarchical data (org chart)
42. Recursive CTE for parent-child relationships
43. Find employees with salary above avg
44. Correlated subquery to find latest order
45. Replace subquery with JOIN
46. Use multiple CTEs in one query
47. Find running total using CTE
48. Compare performance: CTE vs temp table
49. Find cumulative percentage
50. Solve complex multi-step transformation using CTE
💣 MUST-PRACTICE (Most asked in real interviews)
If you’re short on time, focus on these 15:
- #2 Delete duplicates
- #3, #4 Nth highest salary
- #11 Customers with no orders
- #22 Top N per group
- #23 Latest record per group
- #24 Running total
- #25 Moving average
- #28 Difference between rows
- #29 Dedup using window
- #31 Sales aggregation
- #33 Conditional aggregation
- #34 Pivot
- #41 Recursive CTE
- #44 Correlated subquery
Comments
Post a Comment