CROSS JOIN, LATERAL, and APPLY
| Join | What the right side is | Result row count |
|---|---|---|
CROSS JOIN | A table unrelated to the left side | Left rows × right rows |
CROSS JOIN LATERAL, CROSS APPLY | A subquery that references values of the left row | Sum of the rows the subquery returns for each left row |
CROSS JOIN: every combination
CROSS JOIN has no join condition. It attaches every row of the right side to every row of the left side (a Cartesian product).
2 customers × 3 grades
SELECT c.cust_id, g.grade, g.grade_nm
FROM customers c
CROSS JOIN grade_cd g
WHERE c.cust_id BETWEEN 1 AND 2
ORDER BY c.cust_id, g.grade
2 × 3 = 6 rows. The row count is a product, so use it only between small tables.
The row count is a product
SELECT COUNT(*) AS joined_rows
FROM customers c
CROSS JOIN grade_cd g
WHERE c.cust_id <= 10
The result is 10 × 3 = 30. Multiplying all of customers (100,000 rows) by all of orders (2,000,000 rows) gives 200 billion rows. Omitting the join condition in a comma join gives the same result.
Filling in empty combinations
An aggregate result contains only combinations that have data. Customers 101-199 include no VIP customers, so aggregating by grade and status gives no VIP rows. Build every combination first with CROSS JOIN (3 × 3 = 9 rows) and LEFT JOIN the aggregate; empty combinations then remain as 0.
Orders by grade × status (customers 101-199)
SELECT g.grade, s.status, COALESCE(a.orders, 0) AS orders
FROM grade_cd g
CROSS JOIN (VALUES ('PAID'), ('DONE'), ('CANCELED')) AS s(status)
LEFT JOIN (SELECT c.grade, o.status, COUNT(*) AS orders
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.cust_id BETWEEN 101 AND 199
GROUP BY c.grade, o.status) a
ON a.grade = g.grade
AND a.status = s.status
ORDER BY g.grade, s.status
All 9 rows appear, and the three VIP rows are 0. The same form is used to fill gaps by building a calendar or code table, such as date × product (Building a calendar with recursive WITH).
LATERAL: a subquery that references the left row
An inline view in FROM cannot reference other tables in the same FROM. The query below is an error in all three DBs.
-- Error: c is not visible inside the inline view
SELECT c.cust_id, x.ord_id
FROM customers c
CROSS JOIN (SELECT o.ord_id FROM orders o WHERE o.cust_id = c.cust_id) x
Adding LATERAL lets the subquery reference columns of the left table. Read it as recomputing the subquery for each left row. It is the same as writing a correlated subquery in FROM, and it differs from a scalar subquery in that it can return multiple rows and columns.
| Syntax | Oracle | PostgreSQL | SQL Server |
|---|---|---|---|
CROSS JOIN LATERAL (…) | Yes | Yes | No |
LEFT JOIN LATERAL (…) ON TRUE | Yes | Yes | No |
CROSS APPLY (…), OUTER APPLY (…) | Yes | No | Yes |
Latest 3 orders per customer
The most common use is "top N rows per row". Sort inside the subquery and cut it to N rows.
Oracle, PostgreSQL · Latest 3 orders (CROSS JOIN LATERAL)
SELECT c.cust_id, c.cust_nm, r.ord_id, r.ord_dt, r.amt
FROM customers c
CROSS JOIN LATERAL (
SELECT o.ord_id, o.ord_dt, o.amt
FROM orders o
WHERE o.cust_id = c.cust_id
ORDER BY o.ord_dt DESC, o.ord_id DESC
FETCH FIRST 3 ROWS ONLY
) r
WHERE c.cust_id BETWEEN 101 AND 105
ORDER BY c.cust_id, r.ord_dt DESC, r.ord_id DESC
5 customers × 3 orders = 15 rows. Oracle and SQL Server write the same thing with CROSS APPLY. The OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY below is a row limit both DBs accept. If you use only SQL Server, SELECT TOP 3 … also works.
Oracle, SQL Server · Latest 3 orders (CROSS APPLY)
SELECT c.cust_id, c.cust_nm, r.ord_id, r.ord_dt, r.amt
FROM customers c
CROSS APPLY (
SELECT o.ord_id, o.ord_dt, o.amt
FROM orders o
WHERE o.cust_id = c.cust_id
ORDER BY o.ord_dt DESC, o.ord_id DESC
OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY
) r
WHERE c.cust_id BETWEEN 101 AND 105
ORDER BY c.cust_id, r.ord_dt DESC, r.ord_id DESC
TIP The subquery reruns for each customer with
WHERE o.cust_id = c.cust_id. The sandbox'sordershas a(cust_id, ord_dt)index, so when the index order matches theORDER BY, it can read only N rows per customer and stop (Top-N partial range processing). This pays off when the left side has few rows and the right side is large. Conversely, many left rows mean many repetitions (Scalar subquery → aggregate then join).
Keeping rows without a match: LEFT JOIN LATERAL, OUTER APPLY
CROSS JOIN LATERAL and CROSS APPLY discard the left row when the subquery returns 0 rows (like INNER JOIN). To keep it, use the outer join form. LATERAL requires ON, so use the always-true ON TRUE.
Attach the 2 largest canceled orders per customer. A customer with no canceled orders remains as one row with NULL order columns.
Oracle, PostgreSQL · 2 largest canceled orders (LEFT JOIN LATERAL)
SELECT c.cust_id, x.ord_id, x.amt
FROM customers c
LEFT JOIN LATERAL (
SELECT o.ord_id, o.amt
FROM orders o
WHERE o.cust_id = c.cust_id
AND o.status = 'CANCELED'
ORDER BY o.amt DESC, o.ord_id
FETCH FIRST 2 ROWS ONLY
) x ON TRUE
WHERE c.cust_id BETWEEN 101 AND 110
ORDER BY c.cust_id, x.amt DESC
Oracle, SQL Server · 2 largest canceled orders (OUTER APPLY)
SELECT c.cust_id, x.ord_id, x.amt
FROM customers c
OUTER APPLY (
SELECT o.ord_id, o.amt
FROM orders o
WHERE o.cust_id = c.cust_id
AND o.status = 'CANCELED'
ORDER BY o.amt DESC, o.ord_id
OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY
) x
WHERE c.cust_id BETWEEN 101 AND 110
ORDER BY c.cust_id, x.amt DESC
All 10 customers appear. A customer with 2 or more canceled orders gets 2 rows, one with 1 gets 1 row, and one with none gets a single NULL row.
Common to all three DBs: ROW_NUMBER
If you cannot use LATERAL or APPLY, or need the same SQL in all three DBs, number the rows with the window function ROW_NUMBER() and filter.
Latest 3 orders (ROW_NUMBER)
SELECT cust_id, ord_id, ord_dt, amt
FROM (SELECT o.cust_id, o.ord_id, o.ord_dt, o.amt,
ROW_NUMBER() OVER (PARTITION BY o.cust_id
ORDER BY o.ord_dt DESC, o.ord_id DESC) AS rn
FROM orders o
WHERE o.cust_id BETWEEN 101 AND 105) t
WHERE rn <= 3
ORDER BY cust_id, ord_dt DESC, ord_id DESC
The result is the same 15 rows as the CROSS JOIN LATERAL query. The difference is how much is read.
| Method | What is read |
|---|---|
LATERAL, APPLY | Runs the subquery for each left row. With an index, it stops at N rows. |
ROW_NUMBER() | Reads all target rows, numbers them, then filters. |
Other ways to pick the single maximum row per group are compared in Rows with the group-wise maximum.