Multi-Table
Joins
Learn to combine three or more tables in a single SQL query, unlocking the full power of relational databases.
Why Multi-Table Joins?
Real-world databases rarely store everything in one table. Data is normalized: split across many tables to reduce redundancy. A single business question often requires data from three, four, or more tables at once.
Review: The Two-Table Join
Before chaining multiple joins, recall the basic pattern. A JOIN links two tables by matching a column in one to a column in the other.
From Two Tables to Three
Adding a third table is straightforward: chain another JOIN after the first. Each JOIN adds one table and one ON condition.
The Core Pattern
Every multi-table join follows the same template. You simply repeat the JOIN ... ON block for each additional table.
Sample Schema
We will use this four-table schema for all examples. Study the foreign key relationships between tables.
INNER vs LEFT in Chains
You can mix join types freely within a multi-table query. The type you choose for each join controls what happens when there is no match.
| INNER JOIN | LEFT JOIN | |
|---|---|---|
| Unmatched rows | Dropped entirely | Kept with NULLs |
| Row count effect | Can shrink result | Preserves left-side rows |
| Common use | Required relationships | Optional relationships |
| In a chain | Each JOIN filters independently | Later INNERs can nullify earlier LEFTs |
Why Aliases Matter
When multiple tables share column names (like id or name), you must tell SQL which table a column comes from. Aliases keep your query short and readable.
How to Build a Multi-Table Join
Example 1: Four Tables
Goal: List each customer's name and the products they ordered.
How the Engine Processes Joins
SQL processes joins left to right. Each JOIN produces an intermediate result set that feeds into the next join.
Example 2: Adding WHERE and GROUP BY
Goal: Find the total quantity of each product ordered by customers in New York.
Example 3: Mixing LEFT JOIN
Goal: List all customers and their orders, even customers who have never ordered.
Joining a Table to Itself
Sometimes a table has a foreign key pointing back to itself. In a multi-table chain, you can join the same table twice using different aliases.
Common Mistakes
Summary Cheat Sheet
| Concept | Key Detail |
|---|---|
| Number of JOINs | Always \( N - 1 \) JOINs for \( N \) tables |
| Join order | Logically left to right; optimizer may reorder |
| ON condition | Each JOIN needs exactly one ON clause |
| Aliases | Required when column names overlap |
| Mixing types | INNER + LEFT in same query is valid |
| Self-joins | Same table, different aliases |
| Performance | Index foreign key columns for speed |
Question 1 of 5
To join 4 tables together, how many JOIN ... ON clauses do you need?
Question 2 of 5
In MySQL, what happens if you write JOIN table_b without an ON clause?
Question 3 of 5
Query:
SELECT c.name, o.order_date, p.product_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id;
A customer with no orders will appear in the result because of the LEFT JOIN. True or false?
Question 4 of 5
In a self-join, you join a table to itself. What is required to make this work?
Question 5 of 5
Schema: students(student_id, name), enrollments(enrollment_id, student_id, course_id), courses(course_id, title), instructors(instructor_id, name), course_instructors(course_id, instructor_id).
To get each student's name alongside the instructor's name for their courses, how many JOINs are needed?