skip to content

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.

customers
idint4nametext
1Maya Chen
7Ming Li
42Ada Lovelace
the inner query, for customer 1
  1. SELECT o.id, o.status, o.ordered_at::date AS day
  2. FROM orders AS o
  3. WHERE o.customer_id = 1
  4. ORDER BY o.ordered_at DESC
  5. LIMIT 3;
idint4statustextdaydate
9982cancelled2026-06-26
9405delivered2026-06-05
9398delivered2026-06-05
(3 rows)

for Maya Chen, the subquery runs with c.id = 1: her three latest orders, out of 57.

customer 1/4
real output, postgres 18.6 (PGlite 0.5.12) · lateral
step through the customers. for each one, the subquery runs with that customer's id filled in. Ming Li has never ordered, so hers returns nothing, and she's not in the CROSS JOIN LATERAL result at the end.

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:

sqldb=shop
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;
output, from postgres 18.6
ERROR 42P01: invalid reference to FROM-clause entry for table "c"
4 │ WHERE o.customer_id = c.id
at line 4, column 35
DETAIL: There is an entry for table "c", but it cannot be referenced from this part of the query.
HINT: To reference that table, you must mark this subquery with LATERAL.

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:

sqldb=shop
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;
output, from postgres 18.6
SELECT 6
idint4nametextorder_idint4daydate
1Maya Chen99822026-06-26
1Maya Chen94052026-06-05
1Maya Chen93982026-06-05
42Ada Lovelace99462026-06-25
42Ada Lovelace96152026-06-13
42Ada Lovelace82192026-04-23
(6 rows)

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.

predict the output
sql
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;
sqldb=shop
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;
output, from postgres 18.6
ERROR 21000: more than one row returned by a subquery used as an expression

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:

join =
query.sql
  1. 1SELECT c.id, c.name, o.id AS order_id, o.day
  2. 2FROM customers AS c
  3. 3CROSS JOIN LATERAL (
  4. 4 SELECT o.id, o.ordered_at::date AS day
  5. 5 FROM orders AS o
  6. 6 WHERE o.customer_id = c.id
  7. 7 ORDER BY o.ordered_at DESC
  8. 8 LIMIT 3
  9. 9) AS o
  10. 10WHERE c.id IN (1, 7, 42)
  11. 11ORDER BY c.id, o.day DESC, o.id DESC;
the result for CROSS JOIN LATERAL
idint4nametextorder_idint4daydate
1Maya Chen99822026-06-26
1Maya Chen94052026-06-05
1Maya Chen93982026-06-05
42Ada Lovelace99462026-06-25
42Ada Lovelace96152026-06-13
42Ada Lovelace82192026-04-23
(6 rows)

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.

real output, postgres 18.6 (PGlite 0.5.12) · lateral
switch the join. with 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:

sqldb=shop
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;
output, from postgres 18.6
SELECT 3
idint4nametexttagtext
1chef's knifesharp
1chef's knifesteel
2cast iron paniron
(3 rows)

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:

show =
actual rows
the plan for the three customers. the whole subquery (scan, sort, limit) sits on the inner side of a nested loop, with loops=3: once per customer. each run scans all of orders and keeps a couple of rows.

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.