Skip to main content

πŸ—„οΈ 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_number trick.

⚠️ Common Pitfalls​

  • NULL handling: WHERE x != 5 excludes rows where x IS NULL. Use IS DISTINCT FROM or explicit OR 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​