Sub topic 4
LEFT JOIN LATERAL: Keeping Rows with No Match
This chapter assumes that you know basic SELECT, WHERE, ORDER BY, and LIMIT, along with inner joins, left joins, FROM subqueries, and table aliases.
A LEFT JOIN LATERAL is useful when the right side of a join needs values from the current row on the left, but you still want to keep left-side rows that have no matching result. PostgreSQL fills the right-side columns with NULL when the lateral subquery returns no rows.
Why use `LEFT JOIN LATERAL`?
Suppose you want the three most recent orders for every customer. A lateral subquery can look up orders for the current customer:
SELECT c.id, c.name, o.id AS order_id, o.total
FROM customers AS c
CROSS JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC
LIMIT 3
) AS o;CROSS JOIN LATERAL removes a customer if the subquery returns no rows. That behavior is similar to an inner join.
To keep customers who have never placed an order, use LEFT JOIN LATERAL:
SELECT c.id, c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC
LIMIT 3
) AS o ON true
ORDER BY c.id, o.id;The ON true condition means that the subquery itself decides which orders belong to the customer. The condition does not add another restriction. PostgreSQL evaluates the lateral subquery for each customer:
- It passes the current
c.idinto the subquery. - It returns up to three matching orders.
- If it returns no rows,
LEFT JOINcreates one result row withNULLorder columns.
For example, the result might look like this:
| customer_id | customer_name | order_id | total |
|---|---|---|---|
| 1 | Ada | 105 | 42.00 |
| 2 | Lin | 106 | 18.50 |
| 3 | Noor | NULL | NULL |
Customer 3 remains in the result even though no order matches.
Filtering in the right place
A condition inside the lateral subquery controls which rows it produces. This is usually what you want:
SELECT c.id, c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
AND orders.status = 'paid'
ORDER BY created_at DESC
LIMIT 3
) AS o ON true;This keeps customers who have no paid orders.
Be careful when filtering the lateral columns in the outer WHERE clause:
SELECT c.id, c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC
LIMIT 3
) AS o ON true
WHERE o.total > 0;For customers with no orders, o.total is NULL. The condition o.total > 0 is not true, so those customers are removed. The query now behaves like an inner join for that condition.
If you want to keep customers without orders, move the condition into the subquery:
SELECT c.id, c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
AND total > 0
ORDER BY created_at DESC
LIMIT 3
) AS o ON true;Multiple matches and ordering
LEFT JOIN LATERAL does not guarantee one output row per left-side row. If the subquery returns three orders, the customer appears three times. If it returns no orders, the customer appears once with NULL values.
Use a stable tie-breaker when ordering:
SELECT c.id, o.id AS order_id
FROM customers AS c
LEFT JOIN LATERAL (
SELECT id
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC, id DESC
LIMIT 3
) AS o ON true;The second sort column makes the selected rows predictable when two orders have the same created_at.
Exercise
A query uses LEFT JOIN LATERAL, but customers with no orders disappear after adding WHERE o.status = 'paid'. How should you preserve those customers while finding their latest paid orders?
Show answer
Move status = 'paid' into the lateral subquery's WHERE clause, before ORDER BY and LIMIT. The subquery then returns no rows for customers without paid orders, and LEFT JOIN LATERAL preserves them with NULL order columns.