The basic join
Orders store customer_id, while customer names are stored in customers. To see them together, specify how the two tables relate.
1
SELECT o.order_number,
o.ordered_at,
o.total,
c.first_name || ' ' || c.last_name AS customer,
c.country
FROM orders o
JOIN customers c ON c.id = o.customer_id
ORDER BY o.ordered_at DESC
LIMIT 20;
Result — first 5 of 20 rows
| order_number | ordered_at | total | customer | country |
|---|---|---|---|---|
| ORD-2025-002480 | 2025-12-28 23:52:14 | 6333.77 | Elena Meyer | CA |
| ORD-2025-000505 | 2025-12-28 11:16:39 | 2276.51 | Diego Rossi | IT |
| ORD-2025-000940 | 2025-12-28 07:28:46 | 11000.68 | Freya Choi | BR |
| ORD-2025-001581 | 2025-12-28 06:12:38 | 2645.06 | Jonas Lee | JP |
| ORD-2025-000995 | 2025-12-28 05:47:53 | 2584.02 | Leo Haddad | US |
Opens Planche in a separate browser tab, with the sample database loaded and this query ready. Every example on the page reuses that tab, and nothing runs until you press F9.
JOIN on its own means INNER JOIN. ON c.id = o.customer_id is the join condition: it specifies how rows from the two tables match. orders o gives the table a short alias, allowing references such as o.total and c.country.
Aliases stop being optional the moment two tables have a column of the same name. Both of these tables have an id; unqualified, id would be ambiguous and the database would refuse the query.
More than two tables
Joins can be chained. An order reaches its products through two steps: orders → order_items → products.
2
SELECT o.order_number,
p.name AS product,
oi.quantity,
oi.unit_price
FROM orders o
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
WHERE o.status = 'delivered'
ORDER BY o.ordered_at DESC
LIMIT 25;
Result — first 5 of 25 rows
| order_number | product | quantity | unit_price |
|---|---|---|---|
| ORD-2025-000505 | Zen Boots | 1 | 700.65 |
| ORD-2025-000505 | Echo Backpack 40L Plus | 1 | 1570.96 |
| ORD-2025-001581 | Atlas Phone Pro Max | 1 | 1546.38 |
| ORD-2025-001581 | Pulse Headphones | 2 | 548.09 |
| ORD-2025-000995 | Aria Chrono | 2 | 1289.56 |
Each JOIN adds one more table to the result, so a four-table join follows the same pattern with another line. This query reads as orders, their items, and each item's product.
Keeping rows that have no match
An inner join drops anything without a partner. A customer who has never ordered simply disappears from the result. LEFT JOIN keeps every row of the left table and fills the right side with NULL where there was nothing to match.
3
SELECT c.id,
c.first_name,
c.last_name,
o.order_number,
o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
ORDER BY c.id
LIMIT 25;
Result — first 5 of 25 rows
| id | first_name | last_name | order_number | total |
|---|---|---|---|---|
| 1 | Liam | Fischer | ORD-2022-001596 | 1317.56 |
| 1 | Liam | Fischer | ORD-2025-001723 | 2664.56 |
| 1 | Liam | Fischer | ORD-2025-003142 | 4609.77 |
| 2 | Mateo | Costa | ORD-2025-002473 | 3760.6 |
| 3 | Yuki | Jang | ORD-2025-003527 | 7569.3 |
This makes “find what is missing” queries possible: keep the left-side rows, then select only those where the right side is NULL.
4
-- The 95 customers who have never ordered anything.
SELECT c.id, c.first_name, c.last_name, c.country
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL
ORDER BY c.id
LIMIT 25;
Result — first 5 of 25 rows
| id | first_name | last_name | country |
|---|---|---|---|
| 14 | Maya | Moreau | US |
| 17 | Nora | Müller | IT |
| 20 | Sana | Park | KR |
| 30 | Ravi | Park | KR |
| 33 | Oliver | Park | IN |
WHERE o.id IS NULL may look odd because orders.id is a primary key and cannot be NULL in orders. In this result, however, LEFT JOIN supplies NULL for the unmatched right side. Those rows identify the 95 customers who have never ordered.
ON or WHERE: not the same question
Both queries use status = 'cancelled', but they produce different LEFT JOIN results.
5
-- The condition is part of the join: every customer is kept,
-- and only cancelled orders are attached to them.
SELECT c.id, c.first_name, o.order_number, o.status
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'cancelled'
ORDER BY c.id
LIMIT 25;
Result — first 5 of 25 rows
| id | first_name | order_number | status |
|---|---|---|---|
| 1 | Liam | NULL | NULL |
| 2 | Mateo | NULL | NULL |
| 3 | Yuki | ORD-2025-003530 | cancelled |
| 4 | Ines | NULL | NULL |
| 5 | Leo | NULL | NULL |
The condition sits in ON, so it is part of the matching: every customer is kept, and a cancelled order is attached where one exists. Customers with no cancelled order still appear, with NULLs.
6
-- The same condition in WHERE runs after the join and throws away
-- every row where it is not true — including the NULLs the LEFT JOIN made.
SELECT c.id, c.first_name, o.order_number, o.status
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'cancelled'
ORDER BY c.id
LIMIT 25;
Result — first 5 of 25 rows
| id | first_name | order_number | status |
|---|---|---|---|
| 3 | Yuki | ORD-2025-003530 | cancelled |
| 6 | Nora | ORD-2023-001551 | cancelled |
| 6 | Nora | ORD-2022-002224 | cancelled |
| 7 | Priya | ORD-2022-003262 | cancelled |
| 10 | Elena | ORD-2025-000182 | cancelled |
Here the condition is in WHERE, which runs after the join and removes rows for which it is not true. The NULL rows created by LEFT JOIN fail NULL = 'cancelled', so they are discarded. The result is effectively an INNER JOIN.
As a rule of thumb, put a condition on the right-hand table of a LEFT JOIN in ON, and a condition on the left-hand table in WHERE. With an INNER JOIN the two placements give the same result, which can make this distinction easy to miss.
Joining a table to itself
Nothing says the two sides have to be different tables. Employees have a manager_id pointing at another employee, so the table joins to itself under two different aliases.
7
SELECT e.first_name || ' ' || e.last_name AS employee,
e.position,
m.first_name || ' ' || m.last_name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id
ORDER BY manager, employee
LIMIT 25;
Result — first 5 of 25 rows
| employee | position | manager |
|---|---|---|
| Ava Müller | Director | NULL |
| Hana Tanaka | Director | NULL |
| Hugo Ahmed | Director | NULL |
| Lucas Romero | Director | NULL |
| Maya Müller | Director | NULL |
e and m are the same table seen twice, and the aliases are what make it work at all. It is a LEFT JOIN because the ten people at the top have no manager, and an inner join would quietly leave them out.
What you can do now
You can ask questions across the whole schema. What you cannot do yet is summarize the answer: counting the orders, totalling the revenue and averaging are covered in the next lesson.