LEFT & RIGHT JOIN — SQL

LEFT JOIN = INNER JOIN + every row of the left table (the table in FROM) that has no partner. In those extra rows, the right table's columns (the table after…

The INNER JOIN in the last lesson dropped Andi, because Andi has never ordered. Yet sometimes Andi is exactly who you're looking for. For example, the marketing team wants to send a voucher to customers who have never bought anything. A list from an INNER JOIN will never have Andi's name on it.

A LEFT JOIN keeps rows with no partner, like Andi. Its result is the INNER JOIN result plus every row of the left table (the table in FROM) that has no partner. In those extra rows, the right table's columns are filled with NULL (empty). Think of a homework checklist: the teacher writes down every student's name first, then ticks the homework that came in. A student who hasn't submitted anything is still on the list, with an empty homework column. The list of names is like the left table, and the homework that came in is like the rows of the right table.

The data

customers: 1 Budi, 2 Siti, 3 Andi.

orders

id customer_id product
1 1 Laptop
2 1 Mouse
3 2 Monitor
4 NULL Webcam

Two rows have no partner: Andi (never ordered) and the Webcam (a guest buyer's order, customer_id NULL). An INNER JOIN drops both.

Step by step

The query below uses LEFT JOIN in place of INNER JOIN:

SELECT c.name AS customer_name, o.product AS product_name
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id;

Before you look at the tables below, guess: how many rows does the query give, and what's in Andi's product column?

Customer (left) Partners in orders Result rows
Budi Laptop, Mouse Budi, Laptop and Budi, Mouse
Siti Monitor Siti, Monitor
Andi none Andi, NULL

The query result:

customer_name product_name
Budi Laptop
Budi Mouse
Siti Monitor
Andi NULL

The result is 4 rows, and Andi's product is NULL. NULL here means Andi has no partner, not the text "NULL". The Webcam still doesn't appear, because the Webcam is in the right table, and a LEFT JOIN only adds rows of the left table.

RIGHT JOIN and FULL OUTER JOIN

A RIGHT JOIN is the reverse: every row of the right table is kept. The Webcam appears with a NULL name, and Andi is gone. A FULL OUTER JOIN (you may write FULL JOIN) keeps every row of both tables.

JOIN Rows Kept even with no partner
INNER JOIN 3 none
LEFT JOIN 4 Andi (product NULL)
RIGHT JOIN 4 Webcam (name NULL)
FULL OUTER JOIN 5 Andi and the Webcam

So LEFT and RIGHT differ in only one thing: which table keeps all its rows. customers c RIGHT JOIN orders o gives the same result as orders o LEFT JOIN customers c. That's why most people only use LEFT JOIN, and put the tables in the order they need.

On support: SQLite only gained RIGHT and FULL OUTER JOIN in version 3.39 (2022). The Lab, the Playground and the exercise here run SQLite 3.49, so all four JOINs work. PostgreSQL has all four; MySQL has RIGHT JOIN but still no FULL OUTER JOIN.

Finding rows with no partner

The condition IS NULL is true when the column's value is NULL. A LEFT JOIN plus WHERE ... IS NULL leaves only the left rows that have no partner:

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

The result is just Andi: the customer who has never ordered. Why o.id? An order that really exists always has an id. So if o.id is NULL, that row had no partner. Use IS NULL, not = NULL: a comparison with NULL is never true, so WHERE o.id = NULL always gives 0 rows.

📌 Counting with a LEFT JOIN

A LEFT JOIN is also used to count each customer's orders, including customers with 0 orders:

SELECT c.name, COUNT(o.id) AS order_count
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.id;

The result is Budi 2, Siti 1, Andi 0. Careful with COUNT(*): COUNT(*) counts rows, and Andi has one row in the LEFT JOIN result (with NULL in the order columns), so Andi gets 1. COUNT(o.id) skips NULL, so Andi gets 0.

⚠️ Why is Andi still missing?

TL;DR: LEFT JOIN = INNER JOIN + every row of the left table (the table in FROM) that has no partner. In those extra rows, the right table's columns (the table after JOIN) are NULL (empty). From 3 customers and 4 orders: INNER gives 3 rows, LEFT 4 (plus Andi, a customer with no orders, with a NULL product), RIGHT 4 (plus the Webcam, a guest order, with a NULL name), FULL OUTER 5. LEFT and RIGHT only differ in which side keeps all its rows. LEFT JOIN ... WHERE o.id IS NULL finds the left rows with no partner (o is the orders table). Use IS NULL, not = NULL, because a comparison with NULL is never true. To count each customer's orders, use COUNT(o.id), not COUNT(*), so Andi gets 0. Don't filter the right table in WHERE when every left row must stay.

⚡ Keep every left row

Write:

SELECT c.name AS customer_name, o.product AS product_name
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id;

Meaning: A LEFT JOIN is an INNER JOIN plus every row of the left table (the table in FROM, here customers) that has no partner in the right table (orders). c and o are table aliases, short names for customers and orders.

⚡ Andi stays, with NULL

Guess first: the customers are Budi (2 orders), Siti (1 order) and Andi (never ordered). With a LEFT JOIN, how many rows are in the result, and what's in Andi's product column?

Four rows, and Andi's product is NULL, meaning there's no partner. The Webcam, a guest order whose customer_id is NULL, still doesn't appear, because that order is in the right table.

Meaning: For a row with no partner, the right table's columns are filled with NULL (empty).

In the exercise, you need 4 rows with Andi in the customer_name column.

⚡ LEFT, RIGHT, FULL

Meaning: A LEFT JOIN keeps every row of the left table. A RIGHT JOIN keeps every row of the right table, so the Webcam appears with a NULL name. A FULL OUTER JOIN keeps every row of both tables. They only differ in which side keeps all its rows.

⚡ Find the ones with no partner

Write:

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

Meaning: The condition o.id IS NULL is true when the value of o.id is NULL. An order that exists always has an id, so o.id is NULL only on the row of a customer with no orders. The result is just Andi. Use IS NULL, not = NULL, because a comparison with NULL is never true.

What you will learn