Skip to content
Bronze 7 min read · Joins 3 practice problems ↓

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

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

Keep only the guests who have a badge.

Left join

Keep every guest; those without a badge get an empty space.

Full join

Keep every guest and every badge, with empty spaces where there is no partner.

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.

JoinKeepsOrder 107 (no customer)Zoya (no orders)
INNER JOINOnly rows that match on both sidesDroppedDropped
LEFT JOINEvery row of the left tableKept, 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 JOINEvery row of the right tableDroppedKept, order is NULL
FULL JOINEvery row of both tablesKeptKept

INNER JOIN

Spark SQL · Orders with their customer

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
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

Spark SQL · Every order, with its customer if there is one

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
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

Spark SQL · Both orphans appear

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
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:

Spark SQL · Aggregate first, then join: one row per customer

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
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:

Spark SQL · In ON: every customer stays

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
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
Spark SQL · In WHERE: the LEFT JOIN becomes an INNER JOIN

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
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?"

The joined table has more than one row per key, so each revenue row is repeated once per match and summed several times (fan-out). Find it by counting rows per join key on the right side (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?"
For an inner join, none. For a LEFT JOIN, a condition on the right table in ON limits which rows match but keeps every left row; in WHERE it runs after the join and removes the left rows whose right side is NULL, which turns it into an inner join.
Scenario"You must compare yesterday's customer extract with today's and list added, removed and changed customers. Which join?"
A FULL OUTER JOIN on customer_id. A NULL on yesterday's side means added, a NULL on today's side means removed, and a match with different values (compared null-safely, e.g. 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."
It returns 10 billion rows. A cross join pairs every row with every row; use it only for small dimensions, like a calendar against a short list of stores. A join with a missing or always-true condition does the same thing by accident.

Check yourself

4 questions

Orders 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

See all 4 problems →

Keep going

Up next · Joining tables · lesson 2 of 3 · 7 min read
Semi 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