CodeBlanks Logo Icon CodeBlanks

Multi-table JOIN

Join three or more tables in one query.

JOIN and WHERE

After JOIN you can filter the combined result with WHERE.

SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE c.city = 'İstanbul';

This query returns only orders from customers in Istanbul.

o.id, c.name is selected from orders o. customers c is joined on o.customer_id = c.id, keeping only matching rows. WHERE keeps only the rows where c.city = 'İstanbul' holds.

JOIN and WHERE — details

WHERE comes after JOIN and filters the merged table. The JOIN condition goes in ON; the filter goes in WHERE.

Practice JOIN and WHERE

JOIN orders and customers; return only orders from Istanbul customers.

Starter code

query.sql
-- filtered join
SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE

Sample solution

query.sql
SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE c.city = 'İstanbul'

Expected output

id  name
1   Ayşe

Questions and answers

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

1 Filtered join

Question
SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
____ c.city = 'İstanbul';
Answer
SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE c.city = 'İstanbul';

Explanation: After the join, WHERE filters the result; only customers in Istanbul remain.

2 Multi-table JOIN: match the terms

Term Meaning
JOIN join
ON join condition
WHERE filter
FROM table

3 Where does the post-JOIN filter go?

  • GROUP BY
  • ON
  • FROM
  • WHERE Correct answer

Explanation: The filter goes in WHERE.

All lessons in Beginner