Skip to content
Codewilds

Lesson 15 of 30 · Joins

SQL LEFT JOIN: Keep Unmatched Rows and Find Missing Matches

medium20 XP2 min read

An INNER JOIN only keeps rows that found a partner. That silently hides the heroes without a guild, the guild nobody has joined and the item nobody has bought — and very often those are exactly the rows you are looking for. A LEFT JOIN keeps them.

LEFT JOIN keeps every row of the left table

SELECT h.name AS hero, g.name AS guild
FROM heroes h
LEFT JOIN guilds g ON h.guild_id = g.id;
hero guild
Aria Swiftwind Silver Arrows
Borin Ironfist Iron Wall
Cyra Moonweaver Arcane Circle
Dax Shadowstep NULL
… …

All 15 heroes are back. The left table is the one written first — here heroes. Every one of its rows appears in the result; when a row finds no partner, all columns of the right table are filled with NULL. With a plain JOIN, Dax Shadowstep would simply be missing.

The order of the tables matters

Swap the tables and you keep every guild instead:

SELECT g.name AS guild, h.name AS hero
FROM guilds g
LEFT JOIN heroes h ON h.guild_id = g.id;

This returns 13 rows: one per guild member, plus one row Dawn Wardens | NULL for the guild nobody has joined yet. The heroes without a guild are not in this result, because now heroes is the right table.

Finding rows without a match

A LEFT JOIN followed by WHERE <right table column> IS NULL keeps only the rows that found no partner. Which items has nobody ever bought?

SELECT i.name, i.price
FROM items i
LEFT JOIN purchases p ON p.item_id = i.id
WHERE p.id IS NULL;
name price
Crown of Stars 2500

Test a column that can never be NULL in a real match: the right table's id, or the column you joined on. The same pattern finds the guild without members (guilds LEFT JOIN heroes … WHERE h.id IS NULL) or the quest nobody has completed.

Common mistakes

  • = NULL instead of IS NULL. WHERE p.id = NULL is never true, so you get no rows at all.
  • The tables the wrong way round. purchases LEFT JOIN items keeps every purchase, not every item.
  • Filtering the right table in WHERE. WHERE g.city = 'Eldermoor' after a LEFT JOIN throws away the NULL rows again, so it acts like an inner join (6 rows instead of 15). If unmatched rows should stay, put the condition into the join: LEFT JOIN guilds g ON h.guild_id = g.id AND g.city = 'Eldermoor'.

Assignment

The Quest Office wants to send a welcome quest to every hero who has never completed a quest. You need three tables:

  • heroes — id, name, class, level, gold, guild_id, joined
  • guilds — id, name, city, founded
  • completions — hero_id, quest_id, completed_on (one row per completed quest)

For each hero without a single row in completions, return these columns, in this order:

  1. hero — the hero's name
  2. guild — the name of the hero's guild, or NULL if the hero has no guild
  3. level

Sort by hero from A to Z. Heroes without a guild must be in the result too.

Premium lesson

Unlock the full Learn SQL course

  • All 30 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.

Lesson last updated on .

Write code ↓
SQL+20 XP

Lesson 15 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn SQL course

  • All 30 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.