SQL joins and missing rows
A join combines rows by comparing fields such as an ID and a matching foreign key.
How it works
Sketch the match on paper before reading the query. Each book is tried against review rows using its ID. An inner join skips the book with no review. A left join keeps it as a row with missing review fields, which can be useful for a complete book list.
Walk through an example
An inner join keeps matching pairs. A left join also keeps unmatched rows from the left table, with nulls for right-side fields.
What to watch for
A right-side filter in WHERE can remove unmatched rows after a left join. Place a match restriction in ON if keeping unmatched left rows is part of the goal.
Check your understanding
Which books appear when Books is the left side of a left join?
See the example and explanation
- Worked example
- Given
Books A and B exist. Only B has an associated review. - Answer
Both A and B.
B gets its review fields. A remains with null review fields because it had no match.