Sub topic 13
Lateral Subqueries for Per-Row Subselects
Prerequisites and the main idea
This chapter assumes you know SELECT, WHERE, joins, ORDER BY, LIMIT, and NULL. A lateral subquery is a subquery in the FROM clause that can refer to columns from a table that appears before it in the same FROM list.
Without LATERAL, a derived table usually runs independently. With LATERAL, the database evaluates the subquery for each current row from the preceding table.
For example, suppose customers has many orders. You can find each customer's largest order like this:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
CROSS JOIN LATERAL (
SELECT id, total
FROM orders
WHERE customer_id = c.id
ORDER BY total DESC
LIMIT 1
) AS o;The reference c.id connects the inner query to the current customer. The inner query sorts only that customer's orders and keeps the largest one.
CROSS JOIN LATERAL and missing matches
CROSS JOIN LATERAL returns a row only when the lateral subquery returns a row. Therefore, the query above excludes customers who have no orders.
You can think of the evaluation as a repeated process:
To keep customers without orders, use LEFT JOIN LATERAL. The customer remains in the result, and the order columns become NULL.
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN LATERAL (
SELECT id, total
FROM orders
WHERE customer_id = c.id
ORDER BY total DESC
LIMIT 1
) AS o ON TRUE;The condition ON TRUE means that the lateral subquery itself determines whether a matching row exists.
| Goal | Join form | Result when no related row exists |
|---|---|---|
| Return only rows with a match | `CROSS JOIN LATERAL` | The outer row is removed |
| Preserve every outer row | `LEFT JOIN LATERAL ... ON TRUE` | Inner columns are `NULL` |
When lateral is useful
Lateral subqueries are useful when the inner query needs values from the current outer row and must perform row-dependent work such as:
- Selecting the top
Nrelated rows. - Applying a per-row
LIMIT. - Calling a table-valued function with an outer column.
- Computing a small, customized result for each outer row.
A normal join can often solve the same problem, but it may require window functions or an extra grouping step. Lateral syntax can make “for each row, run this query” explicit.
Lateral support varies by database. PostgreSQL supports this syntax. MySQL supports lateral derived tables in version 8.0.14 and later. Check your database documentation before using it.
Trade-offs and performance
A lateral subquery is not automatically faster than a join. It can be efficient when the outer table is selective and the inner table has a useful index, such as an index on (customer_id, total).
It can also be expensive when the database must scan or sort many inner rows once for every outer row. Use EXPLAIN or your database's equivalent query-plan command to check the actual strategy.
Exercise
Which join should you use to return every customer, including customers who have never placed an order, while selecting each customer's largest order?
Show answer
Use LEFT JOIN LATERAL with ON TRUE. The lateral subquery can return the largest order for each customer, and the left join preserves customers whose subquery returns no row.