Joins describe pairs, not magic table merging
Predict the shape of a query result before trusting its aggregate.
What you’ll learn
- Trace an inner join and a left join by hand
- Identify a one-to-many multiplication in an aggregate
- Explain how a WHERE predicate can remove unmatched rows
Start with the meaning of each table
Use a learners table with one row per learner and a completions table with one row per completed lesson. Learner 1 is Mira and learner 2 is Dev. Mira has two completion rows; Dev has none. The learner identifier connects related rows, but the two tables do not have the same grain. Before writing SQL, say what one output row should represent. Many incorrect reports begin by joining reasonable tables without deciding whether the desired result is per learner, per event, or per lesson.
Trace the matching pairs
An inner join on learner identifier creates one row for each matching pair. Mira’s learner row pairs with each of her two completion rows, producing two output rows. Dev produces no pair and is absent. A left join preserves Dev with NULL values for the completion columns, yielding three rows in this example. This does not mean Dev completed a lesson with an unknown title; the NULL values represent the missing right-side match. Name output columns explicitly so the meaning remains clear when tables evolve.
Count the intended thing
To report a count per learner after a left join, group by the learner identifier and count a non-null completion identifier. Counting all output rows would give Dev one because the preserved unmatched row still exists. Counting the completion identifier gives Mira two and Dev zero when real completion identifiers are non-null. The expression matters as much as the join keyword. A tiny example with an unmatched learner is a useful test fixture because it exposes an error a table containing only active learners would hide.
WITH learners(id, name) AS (
VALUES (1, 'Mira'), (2, 'Dev')
), completions(id, learner_id) AS (
VALUES (101, 1), (102, 1)
)
SELECT l.id, l.name, COUNT(c.id) AS completed
FROM learners AS l
LEFT JOIN completions AS c ON c.learner_id = l.id
GROUP BY l.id, l.name
ORDER BY l.id;Watch filters and multiplication
If you place a right-table condition such as completion status equals published in the WHERE clause, the unmatched row does not satisfy it and may disappear. Put a match restriction in the join condition when your intent is to preserve learners without qualifying completions. A second one-to-many join creates another trap: two completions and three bookmarks for Mira can produce six paired rows. Aggregate each relationship at the intended grain before combining counts, or use another explicitly justified query design.
Verify the query as a claim
Run the query on hand-constructed data before a large production dataset. Include no matches, one match, several matches, and a relationship that violates your assumed uniqueness. A foreign key describes a relationship, while a uniqueness constraint can establish that at most one matching row exists for a key. Read both when estimating cardinality. Use an execution plan to investigate performance after the result meaning is correct; an efficiently executed wrong aggregate is still a wrong report.
Try it yourself
Mira has two completions and three bookmarks; Dev has neither. A query left-joins both detail tables to learners and counts completion IDs. What count might it report for Mira, and how can you repair the report?
Show a possible solution
It can report six because each completion pairs with each bookmark. Aggregate completions and bookmarks separately per learner, then join those aggregates to learners, replacing missing counts with zero when that is the desired meaning.
Check your understanding
Try 14 questions across sql and relational databases. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.