Correlated Subqueries in SQL
An inner query that references the outer row
A correlated subquery mentions a column of the outer query, so it cannot run once on its own — it is (conceptually) re-evaluated per outer row. That makes it expressive (“compare each employee to their department’s average”) but potentially slow: naive engines re-run it N times. Good optimisers decorrelate into a join; you should still know what you are asking for.
Alias the outer table
FROM employees e — the alias is what the inner query will reference.
Reference it inside
(SELECT MAX(salary) FROM employees i WHERE i.department = e.department) — e.department is the correlation.
Per-row evaluation (conceptually)
For each employee, the inner query runs with that row’s department bound — N inner executions for N outer rows.
The comparison decides
The outer WHERE compares the row against its group’s inner result — per-department max, average, latest, whatever the inner query computes.
Benchmark against the join
The same logic is often a JOIN + GROUP BY or a window function. Optimisers may decorrelate automatically — EXPLAIN tells you whether they did.
1SELECT e.name, e.department, e.salary2FROM employees e3WHERE e.salary = (SELECT MAX(salary) FROM employees i WHERE i.department = e.department);
| name | department | salary |
|---|---|---|
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
| David | Marketing | 47000 |
For each employee the inner query finds their department’s top salary and compares. Cynthia (91,000) tops Engineering — Alice (85,000) and Farid (73,000) do not match their department’s max. Elena (58,000) tops Sales and David (47,000) tops Marketing, so exactly the three department toppers come back.
Top earner per department
Each row compared to its own group’s maximum.
1SELECT e.name, e.department, e.salary2FROM employees e3WHERE e.salary = (SELECT MAX(salary) FROM employees i WHERE i.department = e.department);
| name | department | salary |
|---|---|---|
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
| David | Marketing | 47000 |
Above own department’s average
The group of reference is per-row, not global.
1SELECT e.name, e.department, e.salary2FROM employees e3WHERE e.salary > (SELECT AVG(salary) FROM employees i WHERE i.department = e.department);
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
Correlated EXISTS — same power, different shape
Many correlated subqueries are existence tests in disguise.
1SELECT e.name2FROM employees e3WHERE EXISTS (SELECT 1 FROM audit a WHERE a.employee_id = e.id AND a.action = 'raise');4-- every employee who has ever had a raise logged
| name |
|---|
| Cynthia |
The JOIN rewrite — same answer, one pass
Per-row subqueries become per-group joins.
1WITH dept_max AS (2 SELECT department, MAX(salary) AS max_sal3 FROM employees GROUP BY department4)5SELECT e.name, e.department, e.salary6FROM employees e7JOIN dept_max dm ON e.department = dm.department AND e.salary = dm.max_sal;
| name | department | salary |
|---|---|---|
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
| David | Marketing | 47000 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.