Joining Multiple Tables & Choosing a Join
Chain three or more tables with INNER or LEFT JOINs, and a simple decision guide to pick the right join every time.
"Show each player with his team and the state his team's city is in." The state lives in a third table, cities. To get there you chain joins: players → teams → cities.
Definition
Joining multiple tables means adding one
JOIN … ON …clause per extra table. SQL processes them left to right: the first two tables are joined, and the result is then joined to the next table, and so on.
- Each join needs its own key: players ↔ teams on
team, teams ↔ cities oncity. - The join type (INNER / LEFT) is chosen separately for each step.
- Start from the main table (the one your question is about) and add the others one by one.
Syntax
sql
SELECT a.col, b.col, c.col
FROM table_a AS a
LEFT JOIN table_b AS b
ON a.key_ab = b.key_ab
LEFT JOIN table_c AS c
ON b.key_bc = c.key_bc;The third table
The setup creates cities (city, state) with 5 cities: Bengaluru–Karnataka, Mumbai–Maharashtra, Chennai–Tamil Nadu, Kolkata–West Bengal, Ahmedabad–Gujarat. Jaipur (RR) and Delhi (DC) are deliberately missing.
Example — INNER JOIN across three tables
sql
SELECT p.name, t.team, c.city, c.state
FROM players AS p
INNER JOIN teams AS t
ON p.team = t.team
INNER JOIN cities AS c
ON t.city = c.city;| name | team | city | state |
|---|---|---|---|
| Virat Kohli | RCB | Bengaluru | Karnataka |
| Rohit Sharma | MI | Mumbai | Maharashtra |
| MS Dhoni | CSK | Chennai | Tamil Nadu |
| Jasprit Bumrah | MI | Mumbai | Maharashtra |
How it works:
- Start with the main table: players (A).
- JOIN teams (B) using the team key → 6 rows.
- JOIN cities (C) using the city key → Buttler (Jaipur) and Warner (Delhi) find no city and are dropped.
INNER keeps only rows that match in every table — 4 of 6 players.
Example — LEFT JOIN across three tables
sql
SELECT p.name, t.team, c.city, c.state
FROM players AS p
LEFT JOIN teams AS t
ON p.team = t.team
LEFT JOIN cities AS c
ON t.city = c.city;| name | team | city | state |
|---|---|---|---|
| Virat Kohli | RCB | Bengaluru | Karnataka |
| Rohit Sharma | MI | Mumbai | Maharashtra |
| Jos Buttler | RR | NULL | NULL |
| MS Dhoni | CSK | Chennai | Tamil Nadu |
| David Warner | DC | NULL | NULL |
| Jasprit Bumrah | MI | Mumbai | Maharashtra |
LEFT keeps all players. Where no match was found, the cells are NULL.
Warning: Once you LEFT JOIN, keep using LEFT JOIN for the following tables.
players LEFT JOIN teams INNER JOIN citiesreturns only 4 rows — the final INNER JOIN throws away the rows the LEFT JOIN kept, because theirt.cityhas no match in cities.
Example — joining facts to dimensions
sql
SELECT p.name, i.match_date, i.runs, t.team_name
FROM innings AS i
INNER JOIN players AS p
ON i.player = p.name
INNER JOIN teams AS t
ON p.team = t.team
WHERE i.runs >= 70
ORDER BY i.runs DESC;| name | match_date | runs | team_name |
|---|---|---|---|
| Jos Buttler | 2025-04-08 | 107 | Rajasthan Royals |
| Virat Kohli | 2025-03-22 | 82 | Royal Challengers Bengaluru |
| Rohit Sharma | 2025-03-30 | 76 | Mumbai Indians |
| Virat Kohli | 2025-04-05 | 73 | Royal Challengers Bengaluru |
| Jos Buttler | 2025-03-25 | 73 | Rajasthan Royals |
How to choose the right join
Ask yourself: which rows do I want?
| Your question | Join |
|---|---|
| Only rows that match in both tables | INNER JOIN |
| All rows of one table (the important one), extras where available | LEFT JOIN (main table first) |
| All rows of both tables | FULL JOIN |
| Rows of one table without a partner | LEFT ANTI JOIN (LEFT JOIN + WHERE b.key IS NULL) |
| Unmatched rows of both tables | FULL ANTI JOIN |
| Every possible combination | CROSS JOIN |
For multi-table queries:
- Pick the main table — the one your question is about ("each player…" → players).
- Decide if every row of it must stay → yes: LEFT JOIN the others; no (only complete matches): INNER JOIN.
- Prefer LEFT over RIGHT; don't mix directions.
Common mistakes
- ❌ Joining C to A when the key is really in B (
ON p.city = c.city— players has no city). ✅ Follow the real key path: players → teams → cities. - ❌ LEFT JOIN followed by INNER JOIN on the optional table. ✅ Keep LEFT all the way.
- ❌ Not counting rows at each step. ✅ Run the query step by step and check the row count after each join.
Interview tip
Q: In A LEFT JOIN B LEFT JOIN C, can a row of A disappear?
A: No — LEFT JOINs never remove rows of A (though a row of A can be repeated if B or C has several matches). Only an INNER join or a WHERE filter on B/C columns can remove it.
Key points to remember
- Add one JOIN ... ON per extra table; SQL joins them left to right, each step joining the previous result to the next table.
- Each step uses its own key: players→teams on team, teams→cities on city.
- INNER JOIN across several tables keeps only rows that match in EVERY table.
- LEFT JOIN across several tables keeps every row of the main table; missing data shows as NULL.
- After a LEFT JOIN, an INNER JOIN on the optional table cancels it out — keep using LEFT.
- Choose joins by asking which rows you want: matching (INNER), all of one (LEFT), all of both (FULL), unmatched (ANTI), all combinations (CROSS).
Try it yourself
INNER JOIN three tables
This example runs a small setup step first — click to see it
CREATE TABLE cities (city VARCHAR(30) PRIMARY KEY, state VARCHAR(30));
INSERT INTO cities (city, state) VALUES
('Bengaluru', 'Karnataka'),
('Mumbai', 'Maharashtra'),
('Chennai', 'Tamil Nadu'),
('Kolkata', 'West Bengal'),
('Ahmedabad', 'Gujarat');Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
LEFT JOIN three tables
This example runs a small setup step first — click to see it
CREATE TABLE cities (city VARCHAR(30) PRIMARY KEY, state VARCHAR(30));
INSERT INTO cities (city, state) VALUES
('Bengaluru', 'Karnataka'),
('Mumbai', 'Maharashtra'),
('Chennai', 'Tamil Nadu'),
('Kolkata', 'West Bengal'),
('Ahmedabad', 'Gujarat');Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Mistake: LEFT then INNER loses rows
This example runs a small setup step first — click to see it
CREATE TABLE cities (city VARCHAR(30) PRIMARY KEY, state VARCHAR(30));
INSERT INTO cities (city, state) VALUES
('Bengaluru', 'Karnataka'),
('Mumbai', 'Maharashtra'),
('Chennai', 'Tamil Nadu'),
('Kolkata', 'West Bengal'),
('Ahmedabad', 'Gujarat');Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: big innings with team names
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
