Course contentsShow
← Data Science
Lesson 950 of 2,14524. SQL JoinsFree lesson

Chaining Multiple Joins

How to join three or more tables in sequence and understand query execution order.

Chaining Multiple Joins

You'll learn how to connect three or more tables in a single query by chaining joins together, and how SQL processes them step-by-step.

The Core Idea

When your data is spread across multiple tables, you often need to join more than two at once. SQL lets you chain joins by adding additional JOIN clauses one after another. Each join builds on the previous result, creating a sequence of connections that gradually brings together all the data you need.

Think of it like connecting train cars: the first JOIN links table A to table B, then the second JOIN attaches table C to that combined result, and so on. Each join creates a new "combined table" that the next join can work with.

Basic Chaining Syntax

SELECT o.order_id, c.customer_name, p.product_name, p.price
FROM orders o
INNER JOIN customers c ON o.customer_id = c.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;

This query chains three joins to connect four tables: orders → customers, orders → order_items, and order_items → products.

Execution Order

SQL processes joins left to right (though the optimizer may reorder them internally). First orders joins with customers, then that result joins with order_items, then finally with products. Each step filters and combines rows based on its ON condition.

Mixing Join Types

You can mix different join types in a chain:

SELECT c.customer_name, o.order_id, p.product_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
LEFT JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id;

The join type you choose for each step determines how unmatched rows are handled at that stage.

Key Takeaway: Chaining joins connects multiple tables sequentially by adding JOIN clauses one after another, with each join building on the combined result of all previous joins.