ποΈ SQL
SQL interview questions test fluency with joins, aggregations, and window functions β not arcane DBA trivia. If you can confidently compose
JOIN,GROUP BY,HAVING, and a window function, you can solve 90% of LeetCode SQL.
This category contains 28 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.
π§ Key Patternsβ
- Joins (INNER, LEFT, SELF) β Combine rows across tables; SELF JOIN for hierarchies.
- GROUP BY + HAVING β Aggregate then filter on the aggregate.
- Window Functions β
ROW_NUMBER,RANK,DENSE_RANK,LAG,LEAD, running sums. - Subqueries / CTEs β
WITH x AS (...)for readability; correlated subqueries for row-by-row logic. - CASE WHEN β Conditional aggregation, pivots.
- Date Arithmetic β Consecutive dates, gaps & islands β
date - row_numbertrick.
β οΈ Common Pitfallsβ
- NULL handling:
WHERE x != 5excludes rows wherex IS NULL. UseIS DISTINCT FROMor explicitOR x IS NULL. - Aggregating without
GROUP BY(or grouping by every selected column). - Vendor differences: MySQL vs PostgreSQL vs SQL Server have different window-function support.
π Study Resourcesβ
πΊ Videosβ
π Booksβ
- SQL Performance Explained β Markus Winand β Best book on indexes & query plans
- SQL for Smarties β Joe Celko β Advanced patterns
π Articles & Referencesβ
π» All SQL Problemsβ
Cinema Seat Allocation
LeetCode 1487 | Difficulty: Medium
Classes More Than 5 Students
LeetCode Link
Combine Two Tables
LeetCode 175 | Difficulty: Easy
Consecutive Numbers
LeetCode 180 | Difficulty: Medium
Count Student Number in Departments
LeetCode Link
Customers Who Never Order
LeetCode 183 | Difficulty: Easy
Daily Temperatures
LeetCode 739 | Difficulty: Medium
Delete Duplicate Emails
LeetCode 196 | Difficulty: Easy
Department Highest Salary
LeetCode 184 | Difficulty: Medium
Department Top Three Salaries
LeetCode 185 | Difficulty: Hard
Duplicate Emails
LeetCode 182 | Difficulty: Easy
Employee Bonus
LeetCode 577 | Difficulty: Easy
Employees Earning More Than Their Managers
LeetCode 181 | Difficulty: Easy
Find Customer Referee
LeetCode 584 | Difficulty: Easy
Managers with at Least 5 Direct Reports
LeetCode 570 | Difficulty: Medium
Maximum Product of Three Numbers
LeetCode 628 | Difficulty: Easy
Maximum Product of Word Lengths
LeetCode 318 | Difficulty: Medium
Not Boring Movies
LeetCode 620 | Difficulty: Easy
Nth Highest Salary
LeetCode 177 | Difficulty: Medium
Rank Scores
LeetCode 178 | Difficulty: Medium
Reorder Data in Log Files
LeetCode 974 | Difficulty: Medium
Replace Employee ID With The Unique Identifier
LeetCode 1509 | Difficulty: Easy
Rising Temperature
LeetCode 197 | Difficulty: Easy
Second Highest Salary
LeetCode 176 | Difficulty: Medium
Sign of the Product of an Array
LeetCode 1950 | Difficulty: Easy
Swap Salary
LeetCode Link
Unique Email Addresses
LeetCode 965 | Difficulty: Easy
Winning Candidate
LeetCode Link