Inner and outer joins
Data is split across tables: orders in one, customers in another. A join brings them back together, row by row. The hard part is not the matching but what happens to rows that have no match, and that is what the different kinds of join decide. This lesson shows each one on the same two small tables.
Foundations · SQL for data engineers · Bronze · Joining tables, lesson 1 of 3
On this page
Read first: SELECT, WHERE and ORDER BY · GROUP BY and HAVING (skip if you know them)
In plain words first
Imagine two lists on a table: a guest list and a list of name badges. You put each guest next to their badge. Some guests have no badge, and some badges belong to nobody who came. What do you do with them?
Inner join
Left join
Full join
Every join below is one of those choices, applied to orders and customers.
Matching rows across tables
A join pairs each row of one table with the rows of another that satisfy a condition, usually equal keys. The two sample tables have the awkward cases on purpose: order 107 was placed by customer 9, who is not in customers, and Zoya (customer 5) has never ordered. The join type decides what happens to rows like these.
| Join | Keeps | Order 107 (no customer) | Zoya (no orders) |
|---|---|---|---|
| INNER JOIN | Only rows that match on both sides | Dropped | Dropped |
| LEFT JOIN | Every row of the left table | Kept, name is NULLNULL: A missing or unknown value. It is not 0 or an empty string, and any comparison with it is unknown instead of true or false. Learn more → | Dropped (orders on the left) |
| RIGHT JOIN | Every row of the right table | Dropped | Kept, order is NULL |
| FULL JOIN | Every row of both tables | Kept | Kept |
INNER JOIN
Input: orders
| order_id | customer_id | restaurant | city | amount | status | placed_at |
|---|---|---|---|---|---|---|
| 101 | 1 | Spice Route | Pune | 420 | delivered | 2026-03-01 12:10:00 |
| 102 | 2 | Pizza Lab | Delhi | 650 | cancelled | 2026-03-01 19:40:00 |
| 103 | 1 | Pizza Lab | Pune | null | delivered | 2026-03-02 13:05:00 |
| 104 | 3 | Spice Route | Pune | 310 | null | 2026-03-03 20:15:00 |
| 105 | 2 | Wok Inn | Delhi | 280 | delivered | 2026-03-04 21:00:00 |
| 106 | 4 | Spice Route | Pune | 540 | delivered | 2026-03-05 13:30:00 |
| 107 | 9 | Wok Inn | Mumbai | 190 | delivered | 2026-03-05 20:45:00 |
| 108 | 1 | Wok Inn | Pune | 360 | refunded | 2026-03-06 19:10:00 |
Input: customers
| customer_id | name | city | signup_date | referred_by |
|---|---|---|---|---|
| 1 | Asha | Pune | 2026-01-05 | null |
| 2 | Ravi | Delhi | 2026-01-20 | 1 |
| 3 | Meera | null | 2026-02-02 | 1 |
| 4 | Kabir | Pune | 2026-02-14 | 2 |
| 5 | Zoya | Mumbai | 2026-03-01 | null |
SELECT o.order_id, o.amount, c.name FROM orders o JOIN customers c ON o.customer_id = c.customer_id ORDER BY o.order_id
JOIN means INNER JOIN. Seven of the eight orders come back: order 107 has no matching customer, so it disappears. If you were totalling revenue, those 190 would vanish from the report without any error. That is why you should always check row counts after a join.
LEFT JOIN: keep every row on the left
Input: orders
| order_id | customer_id | restaurant | city | amount | status | placed_at |
|---|---|---|---|---|---|---|
| 101 | 1 | Spice Route | Pune | 420 | delivered | 2026-03-01 12:10:00 |
| 102 | 2 | Pizza Lab | Delhi | 650 | cancelled | 2026-03-01 19:40:00 |
| 103 | 1 | Pizza Lab | Pune | null | delivered | 2026-03-02 13:05:00 |
| 104 | 3 | Spice Route | Pune | 310 | null | 2026-03-03 20:15:00 |
| 105 | 2 | Wok Inn | Delhi | 280 | delivered | 2026-03-04 21:00:00 |
| 106 | 4 | Spice Route | Pune | 540 | delivered | 2026-03-05 13:30:00 |
| 107 | 9 | Wok Inn | Mumbai | 190 | delivered | 2026-03-05 20:45:00 |
| 108 | 1 | Wok Inn | Pune | 360 | refunded | 2026-03-06 19:10:00 |
Input: customers
| customer_id | name | city | signup_date | referred_by |
|---|---|---|---|---|
| 1 | Asha | Pune | 2026-01-05 | null |
| 2 | Ravi | Delhi | 2026-01-20 | 1 |
| 3 | Meera | null | 2026-02-02 | 1 |
| 4 | Kabir | Pune | 2026-02-14 | 2 |
| 5 | Zoya | Mumbai | 2026-03-01 | null |
SELECT o.order_id, o.amount, c.name FROM orders o LEFT JOIN customers c ON o.customer_id = c.customer_id ORDER BY o.order_id
All eight orders are kept; order 107 gets NULL for every customer column. A RIGHT JOIN is the same thing with the tables swapped, so most people write every outer join as a LEFT JOIN with the table they must keep on the left.
FULL JOIN: keep everything
Input: orders
| order_id | customer_id | restaurant | city | amount | status | placed_at |
|---|---|---|---|---|---|---|
| 101 | 1 | Spice Route | Pune | 420 | delivered | 2026-03-01 12:10:00 |
| 102 | 2 | Pizza Lab | Delhi | 650 | cancelled | 2026-03-01 19:40:00 |
| 103 | 1 | Pizza Lab | Pune | null | delivered | 2026-03-02 13:05:00 |
| 104 | 3 | Spice Route | Pune | 310 | null | 2026-03-03 20:15:00 |
| 105 | 2 | Wok Inn | Delhi | 280 | delivered | 2026-03-04 21:00:00 |
| 106 | 4 | Spice Route | Pune | 540 | delivered | 2026-03-05 13:30:00 |
| 107 | 9 | Wok Inn | Mumbai | 190 | delivered | 2026-03-05 20:45:00 |
| 108 | 1 | Wok Inn | Pune | 360 | refunded | 2026-03-06 19:10:00 |
Input: customers
| customer_id | name | city | signup_date | referred_by |
|---|---|---|---|---|
| 1 | Asha | Pune | 2026-01-05 | null |
| 2 | Ravi | Delhi | 2026-01-20 | 1 |
| 3 | Meera | null | 2026-02-02 | 1 |
| 4 | Kabir | Pune | 2026-02-14 | 2 |
| 5 | Zoya | Mumbai | 2026-03-01 | null |
SELECT c.name, o.order_id FROM customers c FULL OUTER JOIN orders o ON c.customer_id = o.customer_id ORDER BY o.order_id NULLS FIRST
Zoya appears with no order, and order 107 appears with no name. FULL JOIN is the tool for comparing two sources, for example yesterday's extract against today's: rows missing on either side show up as NULLs on that side.
Joins multiply rows
A join returns one row per matching pair. Asha has three orders, so she appears three times. That is correct for a list of orders, but it breaks any total that lives on the customer side. Count before and after joining, and aggregateaggregate: A function that turns many rows into one value: count, sum, avg, min, max. With GROUP BY it gives one value per group. Learn more → the "many" side before joining when you need one row per customer:
Input: orders
| order_id | customer_id | restaurant | city | amount | status | placed_at |
|---|---|---|---|---|---|---|
| 101 | 1 | Spice Route | Pune | 420 | delivered | 2026-03-01 12:10:00 |
| 102 | 2 | Pizza Lab | Delhi | 650 | cancelled | 2026-03-01 19:40:00 |
| 103 | 1 | Pizza Lab | Pune | null | delivered | 2026-03-02 13:05:00 |
| 104 | 3 | Spice Route | Pune | 310 | null | 2026-03-03 20:15:00 |
| 105 | 2 | Wok Inn | Delhi | 280 | delivered | 2026-03-04 21:00:00 |
| 106 | 4 | Spice Route | Pune | 540 | delivered | 2026-03-05 13:30:00 |
| 107 | 9 | Wok Inn | Mumbai | 190 | delivered | 2026-03-05 20:45:00 |
| 108 | 1 | Wok Inn | Pune | 360 | refunded | 2026-03-06 19:10:00 |
Input: customers
| customer_id | name | city | signup_date | referred_by |
|---|---|---|---|---|
| 1 | Asha | Pune | 2026-01-05 | null |
| 2 | Ravi | Delhi | 2026-01-20 | 1 |
| 3 | Meera | null | 2026-02-02 | 1 |
| 4 | Kabir | Pune | 2026-02-14 | 2 |
| 5 | Zoya | Mumbai | 2026-03-01 | null |
SELECT c.name, coalesce(t.orders, 0) AS orders FROM customers c LEFT JOIN ( SELECT customer_id, count(*) AS orders FROM orders GROUP BY customer_id ) t ON t.customer_id = c.customer_id ORDER BY c.name
A filter in ON or in WHERE
For an INNER JOIN it makes no difference. For a LEFT JOIN it changes the result. A condition in ON decides which right-side rows match; every left row is still kept. A condition in WHERE runs after the join and removes rows, including the left rows whose right side is NULL:
Input: orders
| order_id | customer_id | restaurant | city | amount | status | placed_at |
|---|---|---|---|---|---|---|
| 101 | 1 | Spice Route | Pune | 420 | delivered | 2026-03-01 12:10:00 |
| 102 | 2 | Pizza Lab | Delhi | 650 | cancelled | 2026-03-01 19:40:00 |
| 103 | 1 | Pizza Lab | Pune | null | delivered | 2026-03-02 13:05:00 |
| 104 | 3 | Spice Route | Pune | 310 | null | 2026-03-03 20:15:00 |
| 105 | 2 | Wok Inn | Delhi | 280 | delivered | 2026-03-04 21:00:00 |
| 106 | 4 | Spice Route | Pune | 540 | delivered | 2026-03-05 13:30:00 |
| 107 | 9 | Wok Inn | Mumbai | 190 | delivered | 2026-03-05 20:45:00 |
| 108 | 1 | Wok Inn | Pune | 360 | refunded | 2026-03-06 19:10:00 |
Input: customers
| customer_id | name | city | signup_date | referred_by |
|---|---|---|---|---|
| 1 | Asha | Pune | 2026-01-05 | null |
| 2 | Ravi | Delhi | 2026-01-20 | 1 |
| 3 | Meera | null | 2026-02-02 | 1 |
| 4 | Kabir | Pune | 2026-02-14 | 2 |
| 5 | Zoya | Mumbai | 2026-03-01 | null |
SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.status = 'delivered' ORDER BY c.name, o.order_id
Input: orders
| order_id | customer_id | restaurant | city | amount | status | placed_at |
|---|---|---|---|---|---|---|
| 101 | 1 | Spice Route | Pune | 420 | delivered | 2026-03-01 12:10:00 |
| 102 | 2 | Pizza Lab | Delhi | 650 | cancelled | 2026-03-01 19:40:00 |
| 103 | 1 | Pizza Lab | Pune | null | delivered | 2026-03-02 13:05:00 |
| 104 | 3 | Spice Route | Pune | 310 | null | 2026-03-03 20:15:00 |
| 105 | 2 | Wok Inn | Delhi | 280 | delivered | 2026-03-04 21:00:00 |
| 106 | 4 | Spice Route | Pune | 540 | delivered | 2026-03-05 13:30:00 |
| 107 | 9 | Wok Inn | Mumbai | 190 | delivered | 2026-03-05 20:45:00 |
| 108 | 1 | Wok Inn | Pune | 360 | refunded | 2026-03-06 19:10:00 |
Input: customers
| customer_id | name | city | signup_date | referred_by |
|---|---|---|---|---|
| 1 | Asha | Pune | 2026-01-05 | null |
| 2 | Ravi | Delhi | 2026-01-20 | 1 |
| 3 | Meera | null | 2026-02-02 | 1 |
| 4 | Kabir | Pune | 2026-02-14 | 2 |
| 5 | Zoya | Mumbai | 2026-03-01 | null |
SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id WHERE o.status = 'delivered' ORDER BY c.name, o.order_id
The second query loses Meera and Zoya, who have no delivered orders: their o.status is NULL after the join, and WHERE drops them. If the question was "every customer with their delivered orders", the first query is right.
NULL keys never match
A join condition is a comparison, so two NULL keys do not match each other (NULL = NULL is NULL). Rows with NULL keys drop out of inner joins and get no partner in outer joins. When NULL should match NULL, for example composite keys where one part is optional, join with the null-safe <=>: ON a.region <=> b.region.
Common mistakes
- Not checking row counts — An inner join silently drops unmatched rows, and any join can duplicate them. Compare counts before and after every join you are not sure about.
- Summing after a one-to-many join — Customer-level values repeat once per order. Aggregate the many side first, or sum only columns from the many side.
- Filtering the right table in WHERE after a LEFT JOIN — It throws away the unmatched left rows. Put the condition in ON.
- Expecting NULL keys to match — NULL = NULL is not true. Use
<=>(or IS NOT DISTINCT FROM) when NULL keys should pair up.
Key takeaways
- INNER keeps matches only; LEFT keeps every left row; FULL keeps every row of both sides.
- A join returns one row per matching pair, so one-to-many joins repeat the "one" side.
- In a LEFT JOIN, conditions on the right table go in ON; in WHERE they turn it into an inner join.
- NULL keys never match with =. Use <=> when they should.
Interview prep
THE QUESTION
"After someone added a LEFT JOIN to the revenue report, total revenue went up. Nothing else changed. What happened?"
GROUP BY key HAVING count(*) > 1). Fix it by joining on the full unique key, deduplicating the right side, or aggregating it to one row per key before the join. A LEFT JOIN cannot lose left rows, so a rise always points to duplicates.Avoid: "A LEFT JOIN must have added the missing rows." A LEFT JOIN keeps left rows; it can only raise a sum by repeating them, which points to duplicate keys on the right.
Follow-ups · answer out loud, then open
Follow-up"What is the difference between putting a condition in ON and in WHERE?"
Scenario"You must compare yesterday's customer extract with today's and list added, removed and changed customers. Which join?"
NOT (a.email <=> b.email)) means changed. A CASE turns that into a change_type column.Trap"SELECT * FROM a CROSS JOIN b on two 100,000-row tables is a quick way to compare them."
Check yourself
4 questionsOrders has 8 rows; one has a customer_id missing from customers. How many rows does the INNER JOIN return (each customer_id appears once in customers)?
Show the answer
7. The unmatched order is dropped; every other order matches exactly one customer.
Which join keeps unmatched rows from both tables?
Show the answer
FULL OUTER. FULL OUTER JOIN keeps all rows from both sides.
A LEFT JOIN with WHERE right.status = 'x' behaves like which join?
Show the answer
INNER. Unmatched left rows have NULL status, so WHERE removes them.
Do two rows with NULL in the join key match on a.k = b.k?
Show the answer
No. NULL = NULL is NULL, not true. Use <=> to match NULLs.
Practise this
Problems that use this, graded on hidden tests
- Order Lines with Product DetailsJoins Easy
- Employees and Their DepartmentsJoins Medium
- Price Valid at Order Time (Range Join)Joins Hard
Keep going
Up next · Joining tables · lesson 2 of 3 · 7 min readSemi and anti joins
Find rows that have, or lack, a match with EXISTS and LEFT SEMI / ANTI JOIN, and why NOT IN returns nothing once a NULL sneaks in.
Related lessons
Previous: Conditional aggregates and COUNT DISTINCT
Primary sources: JOIN · NULL semantics