SQL & Querying · Learn

The querying spine

18 patterns, ordered from the ground up — each a map of the exact places real queries go wrong, with the one-line truth and a runnable counterexample for every trap.

01Filtering & predicates

WHERE semantics: NULL-aware predicates, timestamp ranges, LIKE vs equality, implicit casts, AND/OR precedence.

6 concepts · 6 lessons
02NULL & three-valued logic

UNKNOWN vs FALSE; IS [NOT] NULL vs =; COALESCE/NULLIF; NULL propagation; aggregates ignore NULLs.

6 concepts · 6 lessons
03Sorting & limiting

ORDER BY determinism, ties at the LIMIT boundary, NULL sort position, ordinal vs expression.

6 concepts · 6 lessons
04Aggregation & GROUP BY

COUNT variants, SUM over empty/all-NULL, WHERE vs HAVING, non-aggregated columns, NULL groups.

8 concepts · 8 lessons
05Joins I — semantics

LEFT-turned-INNER, NULL keys, anti-join three ways, ON vs WHERE in outer joins, self-join.

6 concepts · 6 lessons
06Joins II — fan-out & grain

One-to-many multiplication before aggregation; detecting fan-out; many-to-many; grain as a declared property.

5 concepts · 5 lessons
07Set operations

UNION dedups vs UNION ALL; position-based column alignment; NULL equality for dedup; EXCEPT/INTERSECT.

4 concepts · 4 lessons
08Subqueries & CTEs

Scalar cardinality, correlated vs uncorrelated, EXISTS vs IN, CTE fencing, recursive CTEs.

6 concepts · 6 lessons
09Conditional logic & CASE

Conditional aggregation, CASE evaluation order, CASE in GROUP/ORDER BY, FILTER clause.

6 concepts · 6 lessons
10Windows I — ranking & offsets

ROW_NUMBER/RANK/DENSE_RANK on ties, PARTITION vs GROUP, top-N per group, LAG/LEAD.

6 concepts · 6 lessons
11Windows II — frames & running

ROWS vs RANGE, default-frame peer ties, rolling windows over gapped dates, moving-average edges.

8 concepts · 8 lessons
12Time & dates

Half-open intervals, truncate vs extract, timezones/DST, date spines, week/month boundaries.

8 concepts · 8 lessons
13Deduplication & latest-record

Deterministic latest via complete ORDER BY, DISTINCT vs GROUP BY vs QUALIFY, dedup before/after join.

5 concepts · 5 lessons
14Gaps, islands & sessionization

row_number differencing, gap-to-session boundaries, session metrics, off-by-one at boundaries.

6 concepts · 6 lessons
15Reshaping — pivot & unpivot

Conditional-aggregation pivot, UNION-ALL/lateral unpivot, wide-vs-long as a modeling choice, dynamic-column limits.

4 concepts · 4 lessons
16Data modification & MERGE

Duplicate-source grain, idempotent upsert, DELETE/UPDATE with joins, INSERT-SELECT position risk, transaction batches.

7 concepts · 7 lessons
17Reading performance & plans

Sargability, plan smells (scan/join strategy), SELECT* cost on columnar, spill, row-goal effects.

6 concepts · 6 lessons
18Semi-structured & modern warehouse SQL

JSON extraction, LATERAL/FLATTEN, QUALIFY idiom, array contains/overlap predicates.

8 concepts · 8 lessons