Joining tables
Combine two tables on a key, choose what happens to rows with no match, and find them.
What runs in CodeSplash
- Frequencies (categorical variables) Verified in Python and R
You will be able to
- Join two tables with JOIN and LEFT JOIN
- Predict what each join does to the row count
- Find unmatched rows with an anti join
Read first
Two tables, one key
ReadA join puts columns from two tables side by side, matching rows on a key.
To stay self-contained, this lesson uses a tiny made-up lookup: survey teams for two of the three islands. WITH names it for the query, and you'll learn more about WITH in a later lesson.
WITH team(island, team_name) AS (
VALUES ('Biscoe', 'South team'), ('Dream', 'North team')
)
SELECT p.species, p.island, t.team_name
FROM palmer_penguins_sample AS p
JOIN team AS t ON p.island = t.island
LIMIT 5;
AS p and AS t are short names, and ON says how rows match.
How many rows?
PredictPredict first
The table has 344 penguins: 168 on Biscoe, 124 on Dream and 52 on Torgersen. The lookup has no row for Torgersen. How many rows does the join return?
Reveal
292: . A plain JOIN (an inner join) keeps only rows with a match on both sides. The 52 Torgersen penguins are gone, with no warning. Your averages would now describe a different population, which is the commonest way a join changes an answer.
Count before and after
In the appRun this, then change JOIN to LEFT JOIN and run it again.
WITH team(island, team_name) AS (
VALUES ('Biscoe', 'South team'), ('Dream', 'North team')
)
SELECT count(*) AS n
FROM palmer_penguins_sample AS p
JOIN team AS t ON p.island = t.island;
LEFT JOIN keeps every row of the left table and fills the right side with NULL where nothing matches. So you get 344 rows back. It's the safer default when you're adding columns to a table: the row count can't drop, whatever the lookup is missing.
Find what didn't match
ExploreA LEFT JOIN plus IS NULL shows you the rows that found no partner.
WITH team(island, team_name) AS (
VALUES ('Biscoe', 'South team'), ('Dream', 'North team')
)
SELECT p.island, count(*) AS n
FROM palmer_penguins_sample AS p
LEFT JOIN team AS t ON p.island = t.island
WHERE t.island IS NULL
GROUP BY p.island;
DuckDB also has ANTI JOIN, which says this directly. Replace the FROM to GROUP BY lines with:
WITH team(island, team_name) AS (
VALUES ('Biscoe', 'South team'), ('Dream', 'North team')
)
SELECT count(*) AS n
FROM palmer_penguins_sample AS p
ANTI JOIN team AS t ON p.island = t.island;
Run an unmatched check every time you join on real data.
Keys that almost match
ReadReal keys fail in quiet ways. 'Biscoe' won't match 'biscoe ', because of the case and the trailing space. A number stored as text won't match a number. Neither produces an error; you only get fewer matches.
If your unmatched count is unexpected, inspect those values with DISTINCT, and clean them in a Prepare recipe. Prepare also has a join step that shows you the match rate.
Check your understanding
CheckpointCheck yourself
How many rows does the LEFT JOIN version return?
Show the answer
Answer: 344
Every penguin, with team_name NULL for the 52 on Torgersen.
Check yourself
How many penguins have no matching team?
Show the answer
Answer: 52
All of Torgersen's. If you'd expected zero, you've found a gap in your lookup.
Check yourself
You LEFT JOIN and then add WHERE t.team_name = 'South team'. What's the effect?
Show the answer
Answer: b
WHERE runs after the join. To filter the lookup but keep every left row, put the condition in ON.
Check a real join
ReflectFind a join in your own work. What was the row count before and after? If you can't say, that's the first thing to add. Then summarize the joined column to see who ended up where:
Recap
RecapJOINkeeps only matches, andLEFT JOINkeeps every left row.- Compare row counts before and after every join.
- Find unmatched rows with
LEFT JOIN … IS NULLorANTI JOIN.
Concepts in this lesson