← Back to SQL, page 15

sql-0281 · Hard senior

What is a LATERAL join, and how would you use it to get each customer's two most recent orders?

A LATERAL subquery in FROM can refer to columns of tables listed before it, so it runs once per outer row. In PostgreSQL, FROM customers c CROSS JOIN LATERAL (SELECT ... FROM orders WHERE customer_id = c.id ORDER BY order_date DESC LIMIT 2) gives the top two per customer and can use an index on (customer_id, order_date). SQLite has no LATERAL; a ROW_NUMBER window does the same job.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

PostgreSQL:

sql
SELECT c.name, o.id, o.order_date
FROM customers c
CROSS JOIN LATERAL (
  SELECT id, order_date FROM orders WHERE customer_id = c.id ORDER BY order_date DESC LIMIT 2
) o
ORDER BY c.name, o.order_date DESC;
nameidorder_date
Ana1022026-02-11
Ana1012026-01-05
Bilal1052026-03-09
Bilal1032026-02-14
Chloe1042026-03-01

The subquery sees c.id, which an ordinary derived table cannot. LEFT JOIN LATERAL ... ON true would also keep Dev, who has no orders.

SQLite rejects the keyword:

sql
SELECT c.name, o.id FROM customers c CROSS JOIN LATERAL (SELECT id FROM orders WHERE customer_id = c.id LIMIT 2) o;  -- error on purpose
text
Error: near "SELECT": syntax error

The window version works on both engines:

sql
SELECT name, id, order_date FROM (
  SELECT c.name, o.id, o.order_date,
         ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY o.order_date DESC) AS rn
  FROM customers c JOIN orders o ON o.customer_id = c.id
) WHERE rn <= 2 ORDER BY name, order_date DESC;
nameidorder_date
Ana1022026-02-11
Ana1012026-01-05
Bilal1052026-03-09
Bilal1032026-02-14
Chloe1042026-03-01

LATERAL shines when the per-row subquery is cheap with an index and the outer table is small, because it reads only N rows per customer. The window version ranks every order first.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

-- error on purpose
SELECT c.name, o.id FROM customers c CROSS JOIN LATERAL (SELECT id FROM orders WHERE customer_id = c.id LIMIT 2) o;

SELECT name, id, order_date FROM (
  SELECT c.name, o.id, o.order_date,
         ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY o.order_date DESC) AS rn
  FROM customers c JOIN orders o ON o.customer_id = c.id
) WHERE rn <= 2 ORDER BY name, order_date DESC;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • LATERAL subqueries can reference earlier FROM items
  • They run once per outer row
  • Ideal for top-N per group with an index
  • SQLite lacks LATERAL; use ROW_NUMBER

Common mistakes

  • Using CROSS JOIN LATERAL and losing parents with no rows
  • Expecting a plain derived table to see outer columns

Follow-ups

  • When would the window version be faster?
  • What does LEFT JOIN LATERAL ... ON true do?

Tips

  • Mention the index that makes LATERAL fast