Concept · Combining tables

LEFT JOIN

LEFT JOIN keeps every row from the left table. When it finds no match, it shows NULL in the columns from the right table.

Before continuing: This entry is part of the JOIN guide. If you are new to the topic, start with JOIN.

LEFT JOIN example

Customers with and without orders
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;
Result verified in SQLite
idnameorder_idstatus
1Amy Wilson101shipped
1Amy Wilson103cancelled
1Amy Wilson111pending
6Peter HillNULLNULL

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.

Self-contained query · SQLite
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;
Query result
nameorder_record
Amy101
LiamNULL

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:

Unwanted result
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).