Example 1
Get the two highest-paid employees in each department
For each department row, the lateral subquery runs once with that row's d.id available as a reference. Engineering has three employees; the subquery returns the top two by salary: Ada (120) and Carol (110). Bob (95) is excluded. Finance has two employees so both qualify: Eve (105) and Dave (90). CROSS APPLY on SQL Server behaves identically — it implicitly filters out departments that return zero rows from the inner query, matching JOIN LATERAL … ON TRUE semantics.
CREATE TABLE departments (id INT, name VARCHAR(50));
CREATE TABLE employees (id INT, dept_id INT, name VARCHAR(50), salary INT);
INSERT INTO
departments (id, name)
VALUES
(1, 'Engineering'),
(2, 'Finance');
INSERT INTO
employees (id, dept_id, name, salary)
VALUES
(1, 1, 'Ada', 120),
(2, 1, 'Bob', 95),
(3, 1, 'Carol', 110),
(4, 2, 'Dave', 90),
(5, 2, 'Eve', 105);SELECT
d.name AS department,
e.name AS employee,
e.salary
FROM
departments d
JOIN LATERAL (
SELECT
name,
salary
FROM
employees
WHERE
dept_id = d.id
ORDER BY
salary DESC
LIMIT
2
) e ON TRUE
ORDER BY
d.name,
e.salary DESC;| department | employee | salary |
|---|---|---|
| Engineering | Ada | 120 |
| Engineering | Carol | 110 |
| Finance | Eve | 105 |
| Finance | Dave | 90 |
CREATE TABLE departments (id INT, name VARCHAR(50));
CREATE TABLE employees (id INT, dept_id INT, name VARCHAR(50), salary INT);
INSERT INTO
departments (id, name)
VALUES
(1, 'Engineering'),
(2, 'Finance');
INSERT INTO
employees (id, dept_id, name, salary)
VALUES
(1, 1, 'Ada', 120),
(2, 1, 'Bob', 95),
(3, 1, 'Carol', 110),
(4, 2, 'Dave', 90),
(5, 2, 'Eve', 105);SELECT
d.name AS department,
e.name AS employee,
e.salary
FROM
departments d CROSS APPLY (
SELECT
TOP 2 name,
salary
FROM
employees
WHERE
dept_id = d.id
ORDER BY
salary DESC
) e
ORDER BY
d.name,
e.salary DESC;| department | employee | salary |
|---|---|---|
| Engineering | Ada | 120 |
| Engineering | Carol | 110 |
| Finance | Eve | 105 |
| Finance | Dave | 90 |
MySQL, MariaDB, and PostgreSQL use JOIN LATERAL … ON TRUE with LIMIT 2. SQL Server uses CROSS APPLY with TOP 2. Results are identical.