Skip to the content

Lesson 215 of 296

JOINs

What you will learn

  • Why joins exist
  • INNER JOIN
  • LEFT JOIN and finding missing matches
  • RIGHT, FULL OUTER, CROSS and self joins
  • Joining many tables
  • Where to put conditions: ON vs WHERE
  • How duplicates appear in joins
On this page

Why joins?

Data is split across tables to avoid repetition (Lesson 227): the order stores customer_id, not the customer's name. A join combines rows from two tables using a matching condition, usually foreign key = primary key.

INNER JOIN

Returns only rows that have a match in both tables:

SQL
SELECT o.id AS order_id, c.name, o.status
FROM orders o
JOIN customers c ON c.id = o.customer_id
ORDER BY o.id;
order_idnamestatus
101Aisha Khanpaid
102Aisha Khanpaid
103Ben Smithcancelled
104Chen Lipaid
105Dara Patelpending
106Chen Lipaid

JOIN means INNER JOIN. Eve does not appear: she has no orders, so there is no match.

LEFT JOIN

Returns every row from the left table, with matching rows from the right table, or NULLs when there is no match:

SQL
SELECT c.name, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
ORDER BY c.id, o.id;
nameorder_id
Aisha Khan101
Aisha Khan102
Ben Smith103
Chen Li104
Chen Li106
Dara Patel105
Eve JonesNULL

Finding rows with no match (the anti-join)

"Customers who have never ordered" is one of the most common interview questions:

SQL
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
name
Eve Jones

Same pattern for "products never ordered":

SQL
SELECT p.name
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.id
WHERE oi.order_id IS NULL;
name
Kettle

NOT EXISTS (Lesson 217) is an equally good way to write this.

Other joins

JoinReturns
RIGHT JOINAll rows from the right table. Rarely used: swap the tables and use LEFT JOIN instead
FULL OUTER JOINAll rows from both sides, NULLs where there is no match. Useful for comparing two lists
CROSS JOINEvery combination of rows (5 customers × 5 products = 25 rows). Useful for generating grids, dangerous by accident
Self joinA table joined to itself with two aliases: employees e JOIN employees m ON m.id = e.manager_id to show each employee's manager

Joining many tables

SQL
SELECT o.id, c.name, p.name AS product, oi.quantity,
       oi.quantity * oi.unit_price_pence AS line_total_pence
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
WHERE o.id IN (101, 104)
ORDER BY o.id, p.name;
idnameproductquantityline_total_pence
101Aisha KhanMug21798
101Aisha KhanNotebook1450
104Chen LiMug1899
104Chen LiPen Set22500

ON vs WHERE in a LEFT JOIN

For INNER JOINs it makes no difference. For LEFT JOIN it changes the result:

  • A condition in ON decides which right-side rows match; left rows are still all kept.
  • A condition in WHERE filters the final rows, so a condition on the right table (other than IS NULL) removes the unmatched left rows and turns your LEFT JOIN into an INNER JOIN by accident.

"All customers with their paid orders": put o.status = 'paid' in the ON clause. If you put it in WHERE, customers with no paid orders disappear.

Joins multiply rows

Joining one order to its three items gives three rows. Joining a customer to 2 orders and 5 addresses gives 2 × 5 = 10 rows, and any SUM over that result is wrong (counted several times). Watch for this "fan-out" when joining two one-to-many relationships at once; aggregate each one separately first (in a CTE or subquery, Lesson 217), or use COUNT(DISTINCT ...).

How to say it in an interview

"INNER JOIN returns only matching rows; LEFT JOIN keeps every row from the left table with NULLs where nothing matches, and LEFT JOIN plus WHERE right.id IS NULL finds missing matches, like customers who never ordered. FULL OUTER keeps both sides, CROSS JOIN gives every combination, and a self join links a table to itself, for example employees to managers. In a LEFT JOIN, conditions on the right table belong in ON, because in WHERE they turn it into an inner join. And joining two one-to-many tables at once multiplies rows, so I aggregate separately first."

Key words

WordMeaning
JOIN / INNER JOINRows with a match in both tables
LEFT JOINAll left rows, NULLs where no match
Anti-joinFinding rows with no match
FULL OUTER JOINAll rows from both tables
CROSS JOINEvery combination of rows
Self joinA table joined to itself
Fan-outRows multiplying when joining one-to-many relationships

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.