# Joining Many Tables

> Chain several JOINs to follow keys across four tables, one step at a time, and keep the row count under control.

Subject: SQL | Lesson 16 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/joining-many-tables
Previous: [Self & Cross Joins](https://codewithanimation.in/sql/self-and-cross-joins.md) | Next: [UNION, INTERSECT & EXCEPT](https://codewithanimation.in/sql/set-operations.md)

## One JOIN per link

A real question often crosses more than two tables. You chain joins: each new `JOIN` adds one table and has its own `ON` test. The joins are applied from left to right, so the result of the first join is the input of the second.

A good habit is to follow the foreign keys. A customer has orders, an order has items, and an item points at a product. Each arrow is one join.

## Names instead of ids

Tables store ids, because ids never change. A readable report needs the names behind them. Chained joins swap each id for the row it points at, in one query.

## From customer to product name

```sql
SELECT c.name, o.id AS order_id, i.qty, p.name AS product
FROM customers AS c
JOIN shop_orders AS o ON c.id = o.customer_id
JOIN order_items AS i ON o.id = i.order_id
JOIN products AS p ON i.product_id = p.id;
```

Build it one join at a time and check the result after each. The first join gives 3 rows (one per order). Joining the items gives 4, because order 101 has two items. Joining the products keeps 4 rows and adds the product name.

Every join is an `INNER JOIN` here, so a customer with no orders disappears. If that matters, make the first join a `LEFT JOIN`. Then Chen, Dara and Eli stay in the first step.

## Common mistake: Mixed join types and lost rows

A `LEFT JOIN` followed by an `INNER JOIN` on the same chain can remove the padded rows again, because the NULL key does not match in the next `ON`. If one join in the chain is optional, the joins after it usually need to be optional too.

The row count changes at every step. Check it after each join. A sudden jump means one table has several rows per key.

## Tip: Grow the query one join at a time

Start with two tables, run it, then add the next join and run again. Use one alias per table, and write each `ON` as the key you already have equals the key of the new table.

## Try it: Add one join at a time

Press the three join steps and watch the SQL grow and the rows change. Then switch the first join to LEFT.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/joining-many-tables

## Check yourself

### Question 1

In `A JOIN B ON ... JOIN C ON ...`, what does the second join use as its left input?

- A. The result of A JOIN B
- B. Table A only
- C. Table B only

Correct answer: A

Joins nest from left to right: (A JOIN B) JOIN C.

- Why not B: Joins are applied from left to right, so the second join works on the result of the first.
- Why not C: The left input is the whole result of the first join, not one table.

### Question 2

Order 101 has two items. After joining customers, orders and order_items, how many rows does order 101 make?

- A. 2, one per item
- B. 1, because it is one order
- C. 0, because joins remove duplicates

Correct answer: A

The join pairs the order with each of its items, so one order with two items becomes two rows.

- Why not B: A row is made for each matching pair, so each item makes its own row.
- Why not C: Joins do not remove anything by themselves. Only unmatched rows are dropped.

### Question 3

You want every customer to stay, even those without orders, in a four table chain. Where do you start?

- A. Make the first join (customers to orders) a LEFT JOIN
- B. Make only the last join a LEFT JOIN
- C. Add DISTINCT at the end

Correct answer: A

The customers without orders are lost at the first join, so that is the join that must keep them.

- Why not B: Customers without orders would already be gone after the first INNER JOIN.
- Why not C: DISTINCT removes repeated rows. It cannot bring back rows that a join dropped.

## Remember this

- Chain one JOIN per link, each with its own ON, and follow the foreign keys.
- Joins are applied left to right: the result of one join feeds the next.
- A one-to-many join repeats the parent row once for each child row.
- Add the joins one at a time and check the row count after each.
