CodeBlanks Logo Icon CodeBlanks

Three-table INNER JOIN

Chain JOINs across three tables.

Three-table INNER JOIN

Multiple INNER JOINs chain together. Each JOIN connects the next table with its own ON condition.

SELECT * FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON o.product_id = p.id;

This query joins orders, customers and products.

All columns are selected from orders o. customers c is joined on o.customer_id = c.id, keeping only matching rows. products p is joined on o.product_id = p.id, keeping only matching rows.

Three-table INNER JOIN — details

Each JOIN takes its own ON. Order affects readability, not the result.

For example, joining orders, customers and products shows the customer name and product name side by side on every order row.

Practice Three-table INNER JOIN

Chain orders, customers and products with INNER JOIN.

Starter code

query.sql
-- join three tables
SELECT * FROM orders o
INNER JOIN customers c ON o.customer_id = c.id

Sample solution

query.sql
SELECT * FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON o.product_id = p.id;

Expected output

order_id  customer  product
1         Ayşe      Kalem
2         Mehmet    Defter

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Three-table INNER JOIN: match the terms

Term Meaning
JOIN combine
ON condition
AS alias
WHERE filter

2 Add the third table

Question
SELECT * FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
____ products p ON o.product_id = p.id;
Answer
SELECT * FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON o.product_id = p.id;

Explanation: INNER JOIN is the right answer.

3 How many ON clauses for three tables?

  • Three
  • Two Correct answer
  • One
  • None

Explanation: Two JOINs need two ON conditions.

All lessons in Intermediate