Unit 2/Lesson 5 of 5

SQL You Will Be Expected to Write

The posting says you should be 'as comfortable in SQL query as you are in a partner discovery call.' Learn the five patterns that cover funnel work: dedupe, conditional counts, retention windows, join pitfalls, and session stitching, plus how to sanity-check SQL an AI wrote for you.

SkillsSQL for funnelsJoin pitfallsVerifying generated SQL
+45 XP
✏️ On desktop — select text to ask Nobi, save notes, or add thoughts

Assume these tables

users(userid, enrolledat, language) one row per person
messages(messageid, userid, direction, sentat, loadedat) one row per message; direction is 'in' (from user) or 'out' (from bot)

Examples use BigQuery-style standard SQL (the stack's warehouse); other dialects differ slightly on date functions. If the interviewer's dialect differs, say so and adapt aloud.

Five patterns that cover most funnel work

1. Dedupe (idempotency in SQL). Keep the latest copy of each message:
SELECT * FROM messages QUALIFY ROW_NUMBER() OVER (PARTITION BY message_id ORDER BY loaded_at DESC) = 1
(Where QUALIFY isn't available, wrap in a subquery and filter row number = 1.)

2. Stage counts with conditional aggregation. One pass over data, one row of results:
SELECT COUNT(DISTINCT u.user_id) AS enrolled, COUNT(DISTINCT IF(m.direction = 'in', m.user_id, NULL)) AS replied FROM users u LEFT JOIN messages m ON m.user_id = u.user_id

3. Retention window (day 7 to 13 after enrollment). The denominator is everyone enrolled, the numerator is those with an inbound message in the window:
COUNT(DISTINCT IF(m.direction = 'in' AND m.sent_at >= TIMESTAMP_ADD(u.enrolled_at, INTERVAL 7 DAY) AND m.sent_at < TIMESTAMP_ADD(u.enrolled_at, INTERVAL 14 DAY), u.user_id, NULL))
State the definition aloud and note you should only count users old enough to have reached day 14.

4. Avoid fan-out. Aggregate to the grain you need first, then join:
WITH per_user AS (SELECT user_id, COUNT(*) AS n_in FROM messages WHERE direction = 'in' GROUP BY user_id) SELECT COUNT(*) FROM users u LEFT JOIN per_user p USING (user_id)

5. Session stitching with window functions. Flag a new session after 30 idle minutes, then take a running sum:
SUM(IF(TIMESTAMP_DIFF(sent_at, LAG(sent_at) OVER (PARTITION BY user_id ORDER BY sent_at), MINUTE) > 30 OR LAG(sent_at) OVER (PARTITION BY user_id ORDER BY sent_at) IS NULL, 1, 0)) OVER (PARTITION BY user_id ORDER BY sent_at) gives a per-user session number.

Join pitfalls and how to check SQL you didn't write

Classic traps:
- A WHERE on the right-hand table of a LEFT JOIN (for example WHERE m.direction = 'in') silently turns it into an INNER JOIN, dropping users with no messages and inflating reply rates. Put the condition in the ON clause or in a conditional aggregate.
- Counting COUNT(*) after a one-to-many join counts rows, not users. Use COUNT(DISTINCT user_id) or aggregate first.
- DISTINCT used to hide duplicates instead of fixing the cause.
- Timezones: sent_at in UTC versus a local day boundary.
- NULL handling: NULL never equals anything; use IS NULL.

Checks to narrate when verifying AI-written SQL:
1. What is the grain of each input and of the output?
2. Which join type, and does any filter undo it?
3. Do row counts before and after each join behave as expected?
4. Does COUNT(*) differ from COUNT(DISTINCT key) where it shouldn't?
5. Spot-check one user's rows by hand.
6. Reconcile the total with a number you already trust.

囲
Kosumiこすみ

A diagonal move that looks small but connects stones safely. Small, careful checks (row counts, one user's rows by hand) connect your query to reality before it gets trusted.

Ask Nobiexplain

Select any text in the lesson to ask a question. Press Esc to close.

1 / 5

A query does users LEFT JOIN messages and then adds WHERE m.direction = 'in'. What is the effect?