Concept · Advanced SQL

LATERAL in SQL and APPLY in SQL Server

LATERAL lets a subquery in FROM use columns from the rows that come earlier in that FROM. SQL Server expresses the same idea with APPLY.

Example

We want one row per customer with their most recent order, and we also want to keep the customers who have no orders yet.

PostgreSQL / MySQL 8.4
SELECT c.id,
       c.name,
       last.id AS order_id,
       last.date,
       last.total
FROM customers AS c
LEFT JOIN LATERAL (
  SELECT p.id, p.date, p.total
  FROM orders AS p
  WHERE p.customer_id = c.id
  ORDER BY p.date DESC, p.id DESC
  LIMIT 1
) AS last ON TRUE
ORDER BY c.id;

The subquery sorts only the orders of the customer being processed and keeps the first one. The tiebreaker on p.id DESC makes the choice deterministic if two orders share a date. Peter appears with NULL because LEFT JOIN LATERAL keeps the outer row even when there is no order.

The result was checked against the same teaching data set with an equivalent query using ROW_NUMBER() on the local engine.

How it is used

The reference to t.key inside the subquery is the key part: the derived table depends on the outer row. With LEFT JOIN LATERAL, an outer row can be preserved even if the subquery finds no matches.

What changes from case to case

CROSS JOIN LATERAL vs LEFT JOIN LATERAL

  • CROSS JOIN LATERAL / JOIN LATERAL: if the subquery returns no rows, that outer row disappears from the result.
  • LEFT JOIN LATERAL: keeps the outer row and fills with NULL the columns of the lateral result when there is no match.

The choice resembles the conceptual difference between INNER JOIN and LEFT JOIN: decide whether you need to keep rows with no related data.

SQL Server: CROSS APPLY and OUTER APPLY

SQL Server expresses this pattern with APPLY. CROSS APPLY drops the left row when the right side returns nothing; OUTER APPLY keeps it.

SQL Server
SELECT c.id,
       c.name,
       last.id AS order_id,
       last.date,
       last.total
FROM customers AS c
OUTER APPLY (
  SELECT TOP (1) p.id, p.date, p.total
  FROM orders AS p
  WHERE p.customer_id = c.id
  ORDER BY p.date DESC, p.id DESC
) AS last
ORDER BY c.id;

Do not copy syntax mechanically from one engine to another: the intent is the same, but the keywords and the ways of limiting rows differ.

When to use an alternative

LATERAL is not the only way to get “one related row per group”. A window function with ROW_NUMBER() is usually more portable and can be clearer when you want to rank the whole set before filtering. A correlated scalar subquery also works if you only need to return a single value.

Do not assume LATERAL is faster by definition. Optimisers can rewrite queries and choose different plans depending on indexes, cardinality and data distribution. For performance, compare plans with EXPLAIN or the equivalent tool on your real engine.

Forgetting that the subquery depends on each outer row

If the lateral part returns many rows per customer, the result can grow quickly. Before adding LIMIT, filters or sorting, decide how many rows you really need per outer row.