SQL for data engineers
18 in-depth lessons. The SQL that data engineering interviews test, in Spark SQL: filtering and NULL logic, aggregates, every kind of join, window functions, CTEs and dates, then gaps and islands, sessions, pivots and query plans. Every example is a query you can edit and run in your browser.
Bronze
Module 1: Reading a table
Pick columns, filter rows correctly around NULLs, and derive new values row by row.
- SELECT, WHERE and ORDER BY · Bronze · 7 min read. Pick columns, keep the rows you need and sort them, and the order in which SQL actually runs each clause.
- NULL and three-valued logic · Bronze · 6 min read. Why NULL = NULL is not true, how WHERE quietly drops unknown rows, and the functions that handle NULL safely.
- CASE WHEN and expressions · Bronze · 6 min read. Label, bucket and clean values row by row with CASE, IF, COALESCE and arithmetic.
Module 2: Summarising
Aggregate a table per group, and count only the rows you mean.
- GROUP BY and HAVING · Bronze · 6 min read. Collapse rows into one per group, filter groups with HAVING, and the rule for what SELECT may show.
- Conditional aggregates and COUNT DISTINCT · Bronze · 6 min read. Count and sum only some rows inside one GROUP BY with FILTER, CASE and count_if, and count unique values.
Module 3: Joining tables
Combine tables with the right join, find rows with no match, and compare rows of one table.
- Inner and outer joins · Bronze · 7 min read. Match rows across tables with INNER, LEFT, RIGHT and FULL joins, and see exactly which rows each one keeps.
- Semi and anti joins · Bronze · 7 min read. Find rows that have, or lack, a match with EXISTS and LEFT SEMI / ANTI JOIN, and why NOT IN returns nothing once a NULL sneaks in.
- Self joins · Bronze · 5 min read. Join a table to itself to compare its rows: who referred whom, pairs of orders, and duplicates that differ in one column.
Silver
Module 4: Window functions
Rank rows, compare them with their neighbours, and compute running totals and moving averages.
- Window functions: ranking and top N · Silver · 7 min read. Keep every row while computing over a group, number rows with ROW_NUMBER, RANK and DENSE_RANK, and get the top N per group.
- LAG and LEAD · Silver · 6 min read. Compare each row with the previous or next one: changes, gaps between events and day-over-day growth.
- Running totals and window frames · Silver · 7 min read. Running totals, moving averages and percent of total, and the frame that decides which rows are added up.
Module 5: Structure, sets and dates
Write multi-step queries, compare whole results, and report every day, including empty ones.
- CTEs and subqueries · Silver · 6 min read. Write a long query as named steps with WITH, and use scalar and correlated subqueries where they fit.
- UNION, INTERSECT and EXCEPT · Silver · 6 min read. Stack, intersect and subtract query results, the rules they follow, and reconciling two sources.
- Dates and date spines · Silver · 6 min read. Weeks, months and date arithmetic, half-open ranges, and a calendar that keeps days with no data.
Gold
Module 6: Interview patterns
Solve streak, session and reshaping questions that most candidates find hard.
- Gaps and islands · Gold · 8 min read. Find streaks of consecutive days and runs of the same value with two reusable window tricks.
- Sessionization · Gold · 7 min read. Split a clickstream into sessions with an inactivity timeout, then measure each session.
- Pivot and unpivot · Gold · 6 min read. Turn rows into columns for reports and columns back into rows, with PIVOT, CASE WHEN, UNPIVOT and stack.
Module 7: How a query runs
Read a query plan and explain why a query is slow.
- Reading EXPLAIN · Gold · 8 min read. Read a query plan from the bottom up: scans and pushed filters, shuffles, join strategies, and why a query is slow.