Skip to main content

SQL round

SQL is the one round almost every DE loop has. It's rarely about obscure syntax. It's about getting the right answer on messy data and explaining why it's right.

Format​

Usually 45–60 minutes in a shared editor, sometimes with a real database, often without. Expect 2–4 questions that get harder, with follow-ups like "now make it incremental" or "what if this table has duplicates?". Practice on the playground so you can run things. In the real round, you may have to reason without running.

A method that works for every question​

  1. Restate the output. Columns, one row per what, sort order. "So one row per customer, with their first and second order dates, ordered by customer?"
  2. Ask about the data. Can keys repeat? Can values be NULL? Are timestamps in one time zone? Can events arrive late? Ties?
  3. Plan in steps, out loud. "First dedup, then aggregate per day, then the window." Each step becomes a CTE.
  4. Write it with CTEs. One idea per CTE, named for what it holds (latest_txns, daily_revenue).
  5. Check edge cases. Walk through a NULL, a duplicate, a tie and an empty group. Fix before the interviewer asks.
  6. Talk about scale if there's time. Indexes or clustering, what's expensive, how you'd compute it incrementally.

The patterns​

Most SQL questions are one of these patterns, or two combined. Each links to practice questions.

PatternCore ideaPractice
Top-N per groupROW_NUMBER() OVER (PARTITION BY g ORDER BY x DESC) then filter
DeduplicationNumber copies by a rule, keep 1
Running totals, moving averagesWindow SUM/AVG with an explicit frame
Gaps and islandsRow-number difference labels runs
SessionizationFlag starts with LAG, cumulative-sum the flags
Retention and cohortsPer-user flag first, then aggregate per cohort
FunnelsCarry each step's timestamp forward
PivotConditional aggregation (AVG(CASE …))
SCD Type 2 and point-in-time joinsLEAD to close versions; join on validity range
PercentilesPERCENTILE_CONT … WITHIN GROUP
Self-joins and hierarchiesAlias the same table twice; recursive CTEs
Anti-joinsNOT EXISTS, never NOT IN with NULLs
Date spineGenerate every day, then left join
Period over periodLAG over aggregated periods
As-of / bitemporalLatest row with ts <= cutoff; filter knowledge time first
Fan-out bugsAggregate the child to the parent's grain before joining
Interval overlapa.start < b.end AND b.start < a.end

The mistakes that fail candidates​

  • NOT IN with a NULL in the subquery. It returns no rows. Use NOT EXISTS.
  • COUNT(DISTINCT) to "dedup". It counts keys; it doesn't pick the right copy.
  • Joining before aggregating. One-to-many joins multiply rows and inflate sums and counts.
  • Window frames by accident. ORDER BY without a frame defaults to RANGE … CURRENT ROW in most engines. Write ROWS BETWEEN … when you mean rows.
  • ELSE 0 in a conditional average. It turns "not applicable" into zero.
  • BETWEEN on timestamps with a date upper bound. It misses most of the last day. Use >= start AND < next_start.
  • No tie-breaker. ROW_NUMBER without a unique ordering gives different answers on different runs.

Dialects​

Interviews usually accept any reasonable SQL, but know your target warehouse's quirks. See the dialect gotchas cheatsheet and the window functions cheatsheet. Questions in the bank include dialect notes with links to vendor docs.

Quick check​

What's wrong with this query, which should list customers with no orders?

SELECT customer_id FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders);
Show answer

If any orders.customer_id is NULL (guest checkouts, for example), NOT IN returns no rows at all. x NOT IN (…, NULL) is never true. Use NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id).

All SQL questions​

Further reading​