Ten query shapes answer most SQL questions in a data engineering interview. Each one below has a minimal snippet in standard SQL. Time values are stored as integer seconds (event_ts) or integer day numbers (day_no) where arithmetic is needed, because date arithmetic syntax differs between engines.
WITH ranked AS (
SELECT show_id, episode_id, plays,
ROW_NUMBER() OVER (PARTITION BY show_id ORDER BY plays DESC) AS rn
FROM episodes
)
SELECT show_id, episode_id, plays
FROM ranked
WHERE rn <= 3;ROW_NUMBER returns exactly 3 rows per group. Use DENSE_RANK to keep ties.
WITH ranked AS (
SELECT contact_id, email, updated_at,
ROW_NUMBER() OVER (PARTITION BY contact_id ORDER BY updated_at DESC) AS rn
FROM contacts_raw
)
SELECT contact_id, email, updated_at
FROM ranked
WHERE rn = 1;SELECT card_id, tap_ts, fare,
SUM(fare) OVER (PARTITION BY card_id ORDER BY tap_ts
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_fare
FROM taps;State the frame explicitly. The default frame with ORDER BY treats rows with equal sort values as one group.
SELECT day_no, orders,
AVG(orders) OVER (ORDER BY day_no
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS avg_7d
FROM daily_orders;This assumes one row per day. A missing day makes the window cover more than 7 calendar days.
SELECT day_no, orders,
orders - LAG(orders) OVER (ORDER BY day_no) AS change_from_previous
FROM daily_orders;WITH numbered AS (
SELECT device_id, day_no,
day_no - ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY day_no) AS grp
FROM device_days
)
SELECT device_id, MIN(day_no) AS first_day, MAX(day_no) AS last_day, COUNT(*) AS days
FROM numbered
GROUP BY device_id, grp;Consecutive days share the same difference between the day number and the row number.
WITH flagged AS (
SELECT user_id, event_ts,
CASE WHEN event_ts - LAG(event_ts) OVER (PARTITION BY user_id ORDER BY event_ts) > 1800
THEN 1 ELSE 0 END AS is_new_session
FROM page_views
)
SELECT user_id, event_ts,
SUM(is_new_session) OVER (PARTITION BY user_id ORDER BY event_ts
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS session_no
FROM flagged;A gap above 1,800 seconds starts a new session. Each user's first session is number 0.
SELECT c.customer_id
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1 FROM orders AS o WHERE o.customer_id = c.customer_id
);Prefer NOT EXISTS to NOT IN. If the subquery of a NOT IN returns one null, the result is empty.
SELECT
COUNT(DISTINCT CASE WHEN step = 'quote_started' THEN user_id END) AS started,
COUNT(DISTINCT CASE WHEN step = 'quote_completed' THEN user_id END) AS completed,
COUNT(DISTINCT CASE WHEN step = 'policy_bought' THEN user_id END) AS bought
FROM funnel_events;This counts users at each step without checking order. An ordered funnel compares the time of each step per user.
SELECT s.sale_id, d.region
FROM sales AS s
JOIN dim_store AS d
ON d.store_id = s.store_id
AND s.sale_day >= d.valid_from
AND (s.sale_day < d.valid_to OR d.valid_to IS NULL);Each sale matches the dimension row that was valid on its day. See slowly changing dimensions.
- Grain. State what one output row represents, and confirm no join multiplied the rows.
- Nulls. Check each join key and each aggregated column.
- Ties. Say which ranking function was chosen and why.
- Grokking the Data Engineering Interview teaches each pattern in its Advanced SQL module, plus retention, cohorts, and query plans.
- Lesson: Ranking and Ties, in Grokking the Data Engineering Interview
- Lesson: Gaps and Islands, in Grokking the Data Engineering Interview
- Lesson: Sessionizing Events, in Grokking the Data Engineering Interview
- Grokking SQL for Tech Interviews covers the foundations and has practice problems.
- The SQL round explains what interviewers look for.
- Free answer: SQL query optimization interview questions, which covers the follow-up questions about query speed.