CodeBlanks Logo Icon CodeBlanks

Multi-JOIN report

Write a report joining three tables.

Multi-join report

In this challenge you will chain three tables with INNER JOIN.

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

Three-table join.

o.id, c.name, p.name is 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.

Practice Multi-join report

Join orders, customers and products with INNER JOIN; select ids and names.

Starter code

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

Sample solution

query.sql
SELECT o.id, c.name, p.name
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

id  name   name
1   Ayşe   Kalem

Questions and answers

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

1 How many ON clauses for three tables?

  • None
  • Three
  • Two Correct answer
  • One

Explanation: Two JOINs need two ON clauses.

2 Multi-JOIN report: match the terms

Term Meaning
INNER JOIN intersection
ON condition
three tables chain
alias alias

All lessons in Intermediate