Sub topic 3
CROSS JOIN LATERAL and ON true
This chapter assumes you know SELECT, WHERE, ORDER BY, LIMIT, table aliases, and basic INNER JOIN and LEFT JOIN syntax. It also assumes you have used subqueries in the FROM clause.
The problem: a regular `FROM` subquery cannot see its neighbour
A subquery in FROM normally behaves like an independent table. It cannot refer to an alias defined by another item in the same FROM list.
-- Fails: the subquery cannot refer to c
SELECT c.id, o.total
FROM customers AS c
JOIN (
SELECT total
FROM orders
WHERE orders.customer_id = c.id
) AS o ON true;
-- ERROR: invalid reference to FROM-clause entry for table "c"The subquery tries to use c.id, but PostgreSQL evaluates it as an independent table expression. The LATERAL keyword changes this rule.
What `CROSS JOIN LATERAL` does
A lateral item can refer to columns from items that appear before it in the FROM clause. PostgreSQL evaluates the lateral item once for each row produced by the earlier item.
For example, this query returns the three most recent orders for every 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
ORDER BY c.id, o.id;The reference to c.id is valid because o is lateral and comes after c. PostgreSQL conceptually performs these steps:
- Take one row from
customers. - Substitute that row's
c.idinto the lateral subquery. - Run the subquery.
- Combine the customer row with every row returned by the subquery.
- Repeat for the next customer.
If the subquery returns three rows, the customer appears three times. If it returns no rows, CROSS JOIN LATERAL produces no result for that customer. This is the same row-preserving behavior as an inner join.
`JOIN LATERAL ... ON true`
You can write the same operation with regular JOIN syntax:
SELECT
c.id,
c.name,
o.id AS order_id,
o.total
FROM customers AS c
JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC
LIMIT 3
) AS o ON true;JOIN requires an ON condition. Here, ON true always succeeds, so it does not filter any rows. The actual matching work happens inside the lateral subquery's WHERE clause.
| Form | Needs `ON` | Removes a left row when the lateral item returns no rows |
|---|---|---|
| `CROSS JOIN LATERAL` | No | Yes |
| `JOIN LATERAL ... ON true` | Yes | Yes |
Use CROSS JOIN LATERAL when you want to make the per-row relationship clear. Use JOIN LATERAL ... ON true when you need join syntax for consistency with surrounding joins.
Choosing the result shape
The number of rows returned by the lateral item controls the result:
- One row from the lateral item produces one combined row.
- Several rows produce several combined rows for the left-side row.
- No rows remove the left-side row with a cross or inner lateral join.
This makes CROSS JOIN LATERAL useful when every left-side row must have at least one matching result. If customers without orders must remain in the output, use LEFT JOIN LATERAL ... ON true instead. That form returns NULL for the lateral columns when the subquery finds nothing.
CROSS JOIN LATERAL can also call a set-returning function with values from the left row:
SELECT
p.id,
tag.tag_name
FROM posts AS p
CROSS JOIN LATERAL unnest(p.tags) AS tag(tag_name);Here, unnest turns each post's array into rows. A post with three tags contributes three result rows. A post with an empty array contributes none.
Exercise
What happens to a customer in a CROSS JOIN LATERAL query when the lateral subquery returns no rows?
Show answer
The customer is omitted from the result. Use LEFT JOIN LATERAL ... ON true when the customer must remain.