Skip to content

Joining tables

Combine two tables on a key, choose what happens to rows with no match, and find them.

Applied ~9 min8 steps

What runs in CodeSplash

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

01

Two tables, one key

Read

A 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.

02

How many rows?

Predict

Predict 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?

344, every penguinFewer than 344More than 344
Reveal

292: 168+124168 + 124. 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.

03

Count before and after

In the app

Run 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.

04

Find what didn't match

Explore

A 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.

05

Keys that almost match

Read

Real 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.

06

Check your understanding

Checkpoint

Check 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.

07

Check a real join

Reflect

Find 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:

08

Recap

Recap
  • JOIN keeps only matches, and LEFT JOIN keeps every left row.
  • Compare row counts before and after every join.
  • Find unmatched rows with LEFT JOIN … IS NULL or ANTI JOIN.

Concepts in this lesson