Stop 06 · Joining TablesTopics · 7/10

Joining Multiple Tables

Part of the SQL Roadmap

In short

"Revenue by category" sounds simple until you notice category lives three tables away from the order. Chaining joins lets you walk from orders to items to products to categories in one query.

You can chain joins to pull data from three or more tables, such as orders to order_items to products to categories.

The jaadu passes a brown parcel along a line of courier vans parked end to end, handing it from one open back door to the next on a dusty highway.

Think of it like this

The courier relay

A parcel from Surat to a village near Siliguri doesn't travel in one hop. It passes from the Surat hub to Kolkata to Siliguri to the local delivery boy. Each hand-off works only because both sides scan the same tracking number. A multi-table join is that relay: each JOIN ... ON is one hand-off on a shared key.

Watch it happen

From an order to its category

orders
order_items
products
categories
revenue by category

1/5Start with 1,000 delivered orders from October.

The big ideas

What you need to know

Draw the path first

Sketch the tables and the key that links each pair. The joins then write themselves.

Know the grain

After joining order_items, one row is one item line, not one order. Count orders with COUNT(DISTINCT o.order_id).

Add one join at a time

Check the row count after each join. A surprise jump means a key or relationship is wrong.

Outer joins chain carefully

After a LEFT JOIN, an INNER JOIN on that same right table can quietly drop the rows you kept.

How it works

Build a four-table join safely

  1. 1

    Sketch

    Write the four table names and the key between each pair.

  2. 2

    Start from the grain

    Begin with the table whose rows you want to count, here orders.

  3. 3

    Add one join

    Join order_items and check the row count.

  4. 4

    Repeat

    Add products, then categories, checking the count each time.

  5. 5

    Aggregate last

    Only group and sum once every join is behaving.

See it in code

Walk through the code

sql
1SELECT2  cat.category_name,3  COUNT(DISTINCT o.order_id)        AS orders,4  SUM(oi.quantity)                  AS units,5  SUM(oi.quantity * oi.unit_price)  AS revenue6FROM orders AS o7JOIN order_items AS oi ON oi.order_id    = o.order_id8JOIN products    AS p  ON p.product_id   = oi.product_id9JOIN categories  AS cat ON cat.category_id = p.category_id10WHERE o.status = 'delivered'11  AND o.order_date >= DATE '2026-10-01'12  AND o.order_date <  DATE '2026-11-01'13GROUP BY cat.category_name14ORDER BY revenue DESC;

1/4Hop 1: each order to its item lines. The grain is now one row per item line.

October revenue by category across four tables. Works the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

JOIN products p ON p.product_id = oi.order_id

Do this instead

Matching the wrong columns still runs and returns nonsense. Check each ON joins the same kind of ID.

Oops

COUNT(*) AS orders after joining order_items.

Do this instead

That counts item lines. Use COUNT(DISTINCT o.order_id) to count orders.

Oops

Writing all four joins at once and trusting the total.

Do this instead

Add one join at a time and compare row counts, so you catch a bad join the moment it happens.

Your mission

Four-table revenue

~30 min

Quick check

After joining orders to order_items, what does one row represent?

Go deeper

  1. 1Write a query joining four tables to get revenue per category.
  2. 2Draw the tables and keys before writing the joins.
  3. 3Add joins one at a time and check the row count after each.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge