LATERAL joins
a subquery that can see the row to its left. the latest three orders per customer, in one join.
- address
- everything.joins.lateral_joins
- part
- part 3, joins and subqueries
- chapter
- chapter 9 of 11
- reading time
- 7 min
LATERAL lets a subquery in FROM use columns from the tables to its left. so it runs once per left row, and it can return as many rows as it likes: "the latest three orders of each customer" becomes one join.
CROSS JOIN LATERAL drops a left row whose subquery found nothing. LEFT JOIN LATERAL (...) ON true keeps it, padded.
the picture
you know how a scalar subquery can see the outer row but may only return one value, and a subquery in FROM can return lots of rows but can't see anything outside itself? the one you want for "three latest orders per customer" is the cross of the two: sees the outer row, returns a whole table. that's LATERAL.
think of a personal shopper with a list of customers. for each name on the list, they go to the archive, pull that customer's three newest receipts, and staple them behind the name. a customer with no receipts gets nothing stapled, and whether their name stays on the list depends on which kind of join you asked for.
| idint4 | nametext | |
|---|---|---|
| 1 | Maya Chen | |
| 7 | Ming Li | |
| 42 | Ada Lovelace |
- SELECT o.id, o.status, o.ordered_at::date AS day
- FROM orders AS o
- WHERE o.customer_id = 1
- ORDER BY o.ordered_at DESC
- LIMIT 3;
| idint4 | statustext | daydate |
|---|---|---|
| 9982 | cancelled | 2026-06-26 |
| 9405 | delivered | 2026-06-05 |
| 9398 | delivered | 2026-06-05 |
for Maya Chen, the subquery runs with c.id = 1: her three latest orders, out of 57.
writing one
the first thing everybody tries is a plain subquery in FROM that mentions the customer. postgres refuses, and its hint is the whole chapter:
SELECT c.name, o.id
FROM customers AS c
CROSS JOIN (SELECT o.id FROM orders AS o
WHERE o.customer_id = c.id
ORDER BY o.ordered_at DESC LIMIT 3) AS o
WHERE c.id = 42;a normal FROM item is built on its own, before the join, so it can't refer to c: there's no "current customer" yet. write LATERAL in front and the rules change. the subquery is evaluated for each row to its left, with that row's columns in scope:
SELECT c.id, c.name, o.id AS order_id, o.day
FROM customers AS c
CROSS JOIN LATERAL (
SELECT o.id, o.ordered_at::date AS day
FROM orders AS o
WHERE o.customer_id = c.id
ORDER BY o.ordered_at DESC
LIMIT 3
) AS o
WHERE c.id IN (1, 7, 42)
ORDER BY c.id, o.day DESC, o.id DESC;| idint4 | nametext | order_idint4 | daydate |
|---|---|---|---|
| 1 | Maya Chen | 9982 | 2026-06-26 |
| 1 | Maya Chen | 9405 | 2026-06-05 |
| 1 | Maya Chen | 9398 | 2026-06-05 |
| 42 | Ada Lovelace | 9946 | 2026-06-25 |
| 42 | Ada Lovelace | 9615 | 2026-06-13 |
| 42 | Ada Lovelace | 8219 | 2026-04-23 |
six rows: three for Maya (out of her 57 orders), three for Ada (out of 7), none for Ming Li. the ORDER BY ... LIMIT 3 inside is what makes it "latest three", and it's per customer, because the whole subquery runs once per customer.
SELECT c.name,
(SELECT o.id FROM orders AS o
WHERE o.customer_id = c.id
ORDER BY o.ordered_at DESC LIMIT 3) AS order_id
FROM customers AS c
WHERE c.id = 42;SELECT c.name,
(SELECT o.id FROM orders AS o
WHERE o.customer_id = c.id
ORDER BY o.ordered_at DESC LIMIT 3) AS order_id
FROM customers AS c
WHERE c.id = 42;CROSS or LEFT
the lateral subquery is joined to its row like any other table, so the join type decides what happens when it returns nothing:
- 1SELECT c.id, c.name, o.id AS order_id, o.day
- 2FROM customers AS c
- 3CROSS JOIN LATERAL (
- 4 SELECT o.id, o.ordered_at::date AS day
- 5 FROM orders AS o
- 6 WHERE o.customer_id = c.id
- 7 ORDER BY o.ordered_at DESC
- 8 LIMIT 3
- 9) AS o
- 10WHERE c.id IN (1, 7, 42)
- 11ORDER BY c.id, o.day DESC, o.id DESC;
| idint4 | nametext | order_idint4 | daydate |
|---|---|---|---|
| 1 | Maya Chen | 9982 | 2026-06-26 |
| 1 | Maya Chen | 9405 | 2026-06-05 |
| 1 | Maya Chen | 9398 | 2026-06-05 |
| 42 | Ada Lovelace | 9946 | 2026-06-25 |
| 42 | Ada Lovelace | 9615 | 2026-06-13 |
| 42 | Ada Lovelace | 8219 | 2026-04-23 |
CROSS JOIN LATERAL: each customer joined to whatever their subquery returned. Ming Li's returned nothing, so she has no rows here, like an inner join.
CROSS JOIN LATERAL, Ming Li disappears. with LEFT JOIN LATERAL ... ON true she comes back once, with NULLs where the order would be.ON true looks odd, but it's honest: the subquery already did all the matching inside its own WHERE, so there's nothing left for ON to test. LEFT JOIN needs an ON, so you give it the one that's always true. (it's JOIN LATERAL or LEFT JOIN LATERAL, never RIGHT: the subquery depends on the left side, so the right side can't be the one that's kept.)
functions are lateral already
set-returning functions in FROM, like unnest or generate_series or jsonb_array_elements, may refer to earlier tables without the keyword. postgres treats them as lateral automatically. one row per tag, per product:
SELECT p.id, p.name, t.tag
FROM products AS p
CROSS JOIN LATERAL unnest(p.tags) AS t (tag)
WHERE p.id <= 3
ORDER BY p.id, t.tag;| idint4 | nametext | tagtext |
|---|---|---|
| 1 | chef's knife | sharp |
| 1 | chef's knife | steel |
| 2 | cast iron pan | iron |
product 3, the art of sql, has an empty tag array, so unnest returns no rows for it and the CROSS JOIN drops it, the same way it dropped Ming Li. you'll often see this written with a comma, FROM products AS p, unnest(p.tags) AS t (tag), which is the same query: the comma is a CROSS JOIN, and the function is lateral either way.
what it costs
LATERAL means "once per left row", and the plan says so:
a nested loop, and under it the subquery's own little plan: a scan of orders filtered to one customer, a top-N sort, a limit, all run three times. with no index on orders.customer_id, every run reads all 10,000 orders to find a handful (the dataset has no secondary indexes on purpose). over 2,000 customers that's 20 million rows read. an index on orders (customer_id, ordered_at) turns each run into a short walk down one index: that's multicolumn indexes and indexes for ORDER BY and LIMIT. and when you want the top n of every group, a window function is the other contender, compared on one plan in top n per group.
keylines
- LATERAL lets a FROM subquery use columns from the tables to its left.
- it runs once per left row and can return any number of rows.
- CROSS JOIN LATERAL drops rows with an empty subquery. LEFT JOIN LATERAL ... ON true keeps them.
- functions in FROM (unnest, generate_series) are lateral without the keyword.
- the plan is a nested loop: fast with the right index, a full scan per row without one.