Before continuing: This entry is part of the JOIN guide. If you are new to the topic, start with JOIN.
LEFT JOIN example
SELECT c.id, c.name, p.id AS order_id, p.status
FROM customers AS c
LEFT JOIN orders AS p
ON p.customer_id = c.id
WHERE c.id IN (1, 6)
ORDER BY c.id, p.id;| id | name | order_id | status |
|---|---|---|---|
| 1 | Amy Wilson | 101 | shipped |
| 1 | Amy Wilson | 103 | cancelled |
| 1 | Amy Wilson | 111 | pending |
| 6 | Peter Hill | NULL | NULL |
Amy appears once for each matching order. Peter appears even though he has no orders, and the columns from orders contain NULL.
Check the result with small data
We have two customers, but only one of them has placed an order.
WITH customers(id, name) AS (
VALUES (1,'Amy'), (2,'Liam')
), orders(id, customer_id) AS (VALUES (101,1))
SELECT c.name, p.id AS order_record
FROM customers c LEFT JOIN orders p ON p.customer_id=c.id
ORDER BY c.id;| name | order_record |
|---|---|
| Amy | 101 |
| Liam | NULL |
LEFT JOIN also keeps Liam, with NULL in the order column. That NULL means there is no match in the right table.
Check your understanding: Which row would disappear if you changed LEFT JOIN to INNER JOIN?
Show answer
Liam's row, with a NULL order. INNER JOIN only keeps Amy, who does have a matching order.
How LEFT JOIN is used
The condition ON says how the tables are related. Position matters: swapping the tables changes which set of rows is kept in full.
Common mix-ups with LEFT JOIN
Filtering the right table in WHERE
This query removes customers without orders because p.status = 'shipped' is not true when p.status equals NULL:
FROM customers AS c
LEFT JOIN orders AS p ON p.customer_id = c.id
WHERE p.status = 'shipped'If you want to keep every customer and join only their shipped orders, put the filter in ON: ON p.customer_id = c.id AND p.status = 'shipped'.
Keeping every entity does not mean one row per entity
A left join guarantees that each row from the left source appears, but it can repeat that row once per match. A customer with three orders produces three rows. If you want one summary row per customer, group the details or build a summary before joining. Using an outer join and choosing the final unit of the report are separate decisions.
A missing match shows up as nulls in the right-hand columns, but a null there can also be a genuine value from a row that did match. To know whether there was an order, show a required key from the order. To count orders, count that key rather than the padded row. That way a customer with no orders can show a count of zero in a report that starts from all customers.
Tell two common rules apart: “show every customer with their payments” and “show only customers who have paid”. In the first, the payment filter belongs in ON; in the second, excluding customers with no payment is a valid goal. Include a customer whose orders are all cancelled in your tests: that case exposes filters that seem to keep customers with no orders but drop those without the type of order you were looking for.
Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).