Reference outer query columns inside the subquery.
Section: Subqueries
Correlated subquery
sql
sql
SELECT e.employee_id, e.salary
FROM employees e
WHERE e.salary > (
SELECT AVG(e2.salary)
FROM employees e2
WHERE e2.department_id = e.department_id
);Explanation
Correlated subqueries execute relative to each outer row.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Subqueries