Sub topic 13
LATERAL and CROSS APPLY
Assume you know SELECT, joins, aggregate functions, inline views, and correlated subqueries. This chapter focuses on a special kind of inline view: one that can refer to columns from a table earlier in the FROM clause.
Why ordinary inline views are limited
An inline view is a subquery in the FROM clause. Normally, it cannot refer to a column from a table beside it:
SELECT d.department_name, x.last_name
FROM departments d
JOIN (SELECT last_name, department_id
FROM employees
WHERE department_id = d.department_id) x
ON x.department_id = d.department_id;This statement fails because the inline view cannot see d.department_id in the usual form. A regular inline view is evaluated as an independent query block.
LATERAL changes this rule. It allows the inline view to refer to preceding FROM items. In Oracle Database 12c and later, CROSS APPLY provides the same kind of correlation.
CROSS APPLY for top-N per group
A common requirement is to return the two highest-paid employees in every department. A single global FETCH FIRST 2 ROWS ONLY would return only two employees overall. CROSS APPLY applies the row limit separately for each department:
SELECT d.department_name,
top_emp.last_name,
top_emp.salary
FROM departments d
CROSS APPLY (
SELECT e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id
ORDER BY e.salary DESC, e.employee_id
FETCH FIRST 2 ROWS ONLY
) top_emp
ORDER BY d.department_name, top_emp.salary DESC;The inner query can use d.department_id because d appears before it. Oracle runs the inner query in the context of each department row, orders that department's employees, and keeps two rows.
CROSS APPLY and OUTER APPLY
CROSS APPLY behaves like an inner join. If the inner query returns no rows, the preceding row is absent from the result.
Oracle also supports OUTER APPLY. It keeps the preceding row and returns nulls for columns from the inner query when no match exists. Use it when departments with no employees must still appear.
| Operator | If the inner query returns no rows |
|---|---|
| `CROSS APPLY` | Omits the outer row |
| `OUTER APPLY` | Keeps the outer row with null inner columns |
LATERAL syntax
You can express the same relationship with the LATERAL keyword:
SELECT d.department_name,
top_emp.last_name,
top_emp.salary
FROM departments d,
LATERAL (
SELECT e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id
ORDER BY e.salary DESC, e.employee_id
FETCH FIRST 2 ROWS ONLY
) top_emp
ORDER BY d.department_name, top_emp.salary DESC;CROSS APPLY often makes the join-like behavior clearer. LATERAL emphasizes that the inline view is allowed to reference earlier tables.
Use these features when each outer row needs its own filtered, ordered, or row-limited result. For simple matching conditions, an ordinary join is usually easier to read.
Exercise
Which operator should you use if every department must appear, including departments with no employees?
Show answer
Use OUTER APPLY. It preserves the department row and supplies nulls for columns from the inner query when that query returns no rows.