Sub topic 9
Which Joins Allow LATERAL References
This chapter assumes you know basic SELECT, joins, subqueries in FROM, table aliases, and ORDER BY with LIMIT.
A LATERAL item can refer to tables that appear before it in the FROM clause. In a join tree, the right side can refer to items on the left side. PostgreSQL evaluates the lateral item for each row produced by those earlier items.
The join type matters. PostgreSQL allows a lateral reference from the right side of an INNER JOIN or LEFT JOIN. A CROSS JOIN also works because it is an inner-style join without an ON condition.
Joins that allow the reference
These forms allow the right-hand query to use columns from the left-hand table:
CROSS JOIN LATERALINNER JOIN LATERAL ... ON conditionLEFT JOIN LATERAL ... ON condition
For example, this query uses an INNER JOIN:
SELECT c.id, c.name, o.order_id, o.total
FROM customers AS c
INNER JOIN LATERAL (
SELECT id AS order_id, total
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC
LIMIT 1
) AS o ON true;The subquery refers to c.id, so it is lateral. It runs once for each customer. Because this is an inner join, customers whose subquery returns no row are removed.
ON true means that the lateral subquery already contains the matching condition. The join condition does not need to add another filter.
A comma followed by LATERAL is also commonly used:
SELECT c.id, o.order_id
FROM customers AS c,
LATERAL (
SELECT id AS order_id
FROM orders
WHERE orders.customer_id = c.id
LIMIT 1
) AS o;CROSS JOIN LATERAL is clearer because it shows the intended join operation explicitly.
Why RIGHT and FULL joins cause errors
A lateral item on the right side of a RIGHT JOIN or FULL JOIN cannot use columns from the left side. PostgreSQL rejects this type of reference because those join types can null-extend the left side. The lateral expression would otherwise depend on rows that might not exist as ordinary input rows.
This query produces an error:
SELECT c.id, o.order_id
FROM customers AS c
RIGHT JOIN LATERAL (
SELECT id AS order_id
FROM orders
WHERE orders.customer_id = c.id
) AS o ON true;The problem is not the WHERE condition. The problem is that the lateral item is on the right side of a RIGHT JOIN, and it tries to read c.id.
A FULL JOIN has the same restriction when the right-hand lateral item refers to the left side:
SELECT c.id, o.order_id
FROM customers AS c
FULL JOIN LATERAL (
SELECT id AS order_id
FROM orders
WHERE orders.customer_id = c.id
) AS o ON true;PostgreSQL reports that the combining join type must be INNER or LEFT for this lateral reference.
Fixing an invalid join
Start by identifying the table that drives the lateral lookup. Put that table on the left, then use LEFT JOIN LATERAL if you need to preserve every driving row.
SELECT o.id AS order_id, c.id AS customer_id, c.name
FROM orders AS o
LEFT JOIN LATERAL (
SELECT id, name
FROM customers
WHERE customers.id = o.customer_id
) AS c ON true;Here, o is the driving table and appears on the left. The lateral subquery can safely refer to o.customer_id. The LEFT JOIN keeps orders even when no matching customer exists.
Often, a lateral lookup can be replaced by an ordinary join. This version is simpler because the lookup returns at most one customer row:
SELECT o.id AS order_id, c.id AS customer_id, c.name
FROM orders AS o
LEFT JOIN customers AS c
ON c.id = o.customer_id;Use LATERAL when the right side needs per-row behavior such as LIMIT, ordering, a set-returning function, or a calculation that depends on the left row.
A practical rule
Read a lateral join from left to right:
- Find the table or join result that supplies the input row.
- Put that input on the left.
- Put the dependent subquery or function on the right.
- Use
CROSSorINNERwhen rows without a result should disappear. - Use
LEFTwhen every input row must remain.
Exercise
Why does LEFT JOIN LATERAL work for a subquery that refers to the left table, while RIGHT JOIN LATERAL produces an error?
Show answer
PostgreSQL permits a right-hand lateral item to reference the left side for INNER and LEFT joins, but not for RIGHT or FULL joins. Rewrite the query so the driving table is on the left of a LEFT JOIN LATERAL.