Lesson 14 of 30 · Joins
SQL Multiple Joins and Table Aliases: Join Three Tables
Some questions need more than two tables. Which hero completed which quest? The heroes live in one table, the quests in another, and a third table, completions, connects them. This lesson shows how to chain several joins, and how table aliases keep long queries readable.
Table aliases
Writing heroes.name and guilds.name again and again gets long. Give each table a short alias after its name and use the alias everywhere in the query:
SELECT h.name AS hero, g.name AS guild
FROM heroes AS h
JOIN guilds AS g ON h.guild_id = g.id;
The AS is optional — FROM heroes h works too. Common aliases are the first letter of the table name. Once a table has an alias, you must use it: heroes.name in this query fails with no such column: heroes.name.
Link tables
A hero can complete many quests, and a quest can be completed by many heroes. Neither table can store "the" id of the other, so a link table stores one row per pair:
| hero_id | quest_id | completed_on |
|---|---|---|
| 1 | 1 | 2025-01-20 |
| 1 | 3 | 2025-02-11 |
| 2 | 2 | 2024-11-20 |
| … | … | … |
Both hero_id and quest_id are foreign keys: Aria Swiftwind (hero 1) finished Rats in the Cellar (quest 1) and Wolves of Greywood (quest 3). The purchases table works the same way for heroes and items, with a quantity and a date as extra information.
Joining three tables
Start from the link table and join each side with its own JOIN … ON:
SELECT h.name AS hero, q.title AS quest, c.completed_on
FROM completions c
JOIN heroes h ON c.hero_id = h.id
JOIN quests q ON c.quest_id = q.id
WHERE q.difficulty = 'hard'
ORDER BY c.completed_on;
| hero | quest | completed_on |
|---|---|---|
| Hugo Stonebeard | The Bandit King | 2024-06-01 |
| Hugo Stonebeard | The Clockwork Golem | 2024-08-12 |
| Cyra Moonweaver | The Drowned Library | 2024-09-30 |
| … | … | … |
The joins build up step by step: first every completion gets its hero, then that combined row gets its quest. You can keep going the same way — JOIN guilds g ON h.guild_id = g.id would add each hero's guild as a fourth table.
Common mistakes
- A missing
ON. EveryJOINneeds its own condition. Leave out the one forheroesabove and every completion is paired with every hero: 435 rows instead of 10. - Mixing aliases and full names. After
FROM heroes h, writeh.name, notheroes.name. - Joining the wrong columns.
ON c.quest_id = h.idruns without an error but matches quest numbers with hero numbers. Check that every foreign key is joined to the table it points to.
Assignment
The market keeper wants to know who buys the valuable goods. Use these three tables:
purchases—id,hero_id,item_id,quantity,bought_onheroes—id,name,class,level,gold,guild_id,joineditems—id,name,type,price,rarity(common,rare,epicorlegendary)
For every purchase of an item that is not common, return these columns, in this order:
hero— the buyer's nameitem— the item's namerarity— the item's raritybought_on— the purchase date
Sort by hero from A to Z, and each hero's purchases from oldest to newest. For example, Hugo Stonebeard bought the Dragonscale Mail (legendary) on 2024-05-10 and the Elixir of Giants (epic) on 2024-12-23.
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
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 .