SQL Interview Challenges

Solving timed SQL problems on joins, aggregation, window functions and dates, and talking through your approach clearly.

Intermediate · about 15 hours · difficulty 3/5

Learn first: Window Functions, Dates, Times and Text in SQL

Nearly every analyst interview includes live or timed SQL. The questions repeat a small set of patterns, so deliberate practice pays off quickly.

Patterns to master

  • Top N per group: ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC) then filter rn <= 3.
  • Running totals and moving averages with window frames.
  • Period over period: LAG() for month-over-month growth.
  • Gaps and islands / consecutive days: subtract a row number from the date to group streaks.
  • Anti-joins: customers with no orders using LEFT JOIN ... WHERE o.id IS NULL or NOT EXISTS.
  • Conditional aggregation: SUM(CASE WHEN ... THEN 1 ELSE 0 END) to pivot.
  • Retention and funnels: first event per user, then join back.
  • Deduplication: keep the latest row per key.

How to approach a question live

  1. Clarify: grain of each table, duplicates, nulls, ties, time zone.
  2. Describe the plan out loud before typing.
  3. Build step by step with CTEs, checking intermediate results.
  4. Test edge cases: empty groups, ties, nulls.
  5. Mention performance if tables are large.

Practice routine

Solve two or three problems daily for a few weeks, mixing easy and medium. Time yourself (15-20 minutes each), and rewrite solutions you struggled with a few days later from scratch.

Pitfall: silently writing a huge query and debugging it in panic. Narrate and build in small pieces. You are ready to move on when you can solve medium window-function problems in under 20 minutes while explaining your reasoning.

Key concepts

  • Top N per group
  • Running totals and moving averages
  • LAG and period-over-period
  • Gaps and islands
  • Anti-joins and NOT EXISTS
  • Conditional aggregation
  • Deduplication patterns
  • Thinking aloud and clarifying

Mini project

Solve 40 SQL problems over four weeks across the eight patterns, keep a notebook of each pattern with your cleanest solution, and do two mock interviews with a friend.

Check yourself

Write, from memory, a query returning each customer's second most recent order.

Free resources