Lesson 13 of 30 · Joins
SQL INNER JOIN: Combine Rows from Two Tables
Look at the guild_id column of heroes: Aria Swiftwind is in guild 1. But which guild is that, and where is it based? The answer lives in a different table. A join brings related rows from several tables together into one result — after SELECT itself, it is the most important skill in SQL.
Why data is split into tables
The database could store the guild's name and city in every hero's row. Then "Silver Arrows, Eldermoor" would be repeated for every member, a typo could create a second, slightly different guild, and renaming a guild would mean changing many rows.
Instead, every fact is stored once. Each guild has a row in guilds with a unique id — its primary key. Heroes point to their guild by storing that id in guild_id, which is called a foreign key:
| id | name | city | founded |
|---|---|---|---|
| 1 | Silver Arrows | Eldermoor | 1987 |
| 2 | Iron Wall | Stonehold | 1952 |
| 3 | Arcane Circle | Mistvale | 1903 |
| 4 | Night Market | Eldermoor | 2011 |
| 5 | Dawn Wardens | Sunreach | 2023 |
| id | name | … | guild_id |
|---|---|---|---|
| 1 | Aria Swiftwind | … | 1 |
| 2 | Borin Ironfist | … | 2 |
| 4 | Dax Shadowstep | … | NULL |
JOIN … ON
JOIN names the second table, and ON says which rows belong together:
SELECT heroes.name AS hero, guilds.name AS guild, guilds.city
FROM heroes
JOIN guilds ON heroes.guild_id = guilds.id;
| hero | guild | city |
|---|---|---|
| Aria Swiftwind | Silver Arrows | Eldermoor |
| Borin Ironfist | Iron Wall | Stonehold |
| Cyra Moonweaver | Arcane Circle | Mistvale |
| … | … | … |
For every hero, the database looks for the guild whose id equals the hero's guild_id and glues the two rows together. After that you can use columns from both tables anywhere — in WHERE guilds.city = 'Eldermoor', in ORDER BY, or in calculations.
Qualifying column names
Both tables have an id and a name column, so table.column tells the database which one you mean. An unqualified name in this query fails with ambiguous column name: name. Columns that exist in only one table may be written without the table name, but qualifying every column makes joins much easier to read.
Only matching rows
JOIN is short for INNER JOIN: it keeps only rows that found a partner. The query above returns 12 heroes, not 15 — Dax, Iris and Kira have no guild, and NULL never equals anything. The Dawn Wardens are missing too, because no hero points to them. The next lessons show how to keep such rows.
Common mistakes
- Forgetting
ON.SELECT * FROM heroes JOIN guilds;pairs every hero with every guild: 15 × 5 = 75 rows of nonsense. - Joining the wrong columns.
ON heroes.id = guilds.idruns without an error, but it pairs hero 1 with guild 1 just because the numbers happen to match. Always join a foreign key to the id it points to. - Ambiguous names. Qualify columns that exist in both tables, such as
nameandid.
Assignment
The market keeper wants a ledger of all potion sales. Purchases are stored in purchases, and item_id points to a row in items:
| id | hero_id | item_id | quantity | bought_on |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 2025-01-13 |
| 2 | 1 | 8 | 3 | 2025-01-20 |
| 4 | 2 | 8 | 5 | 2025-01-04 |
| … | … | … | … | … |
| id | name | type | price | rarity |
|---|---|---|---|---|
| 8 | Healing Potion | potion | 25 | common |
| 9 | Mana Potion | potion | 30 | common |
| 10 | Elixir of Giants | potion | 300 | epic |
| … | … | … | … | … |
For every purchase of an item whose type is 'potion', return these columns, in this order:
bought_on— the purchase datepotion— the name of the itemquantitycost— the item's price times the quantity
Sort the rows by bought_on, oldest first. For example, purchase 2 is 3 Healing Potions for a cost of 75.
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 .