Sub topic 7
Correlated Subqueries
This chapter assumes you know basic SELECT, WHERE, joins, aggregate functions, and the Oracle HR sample tables EMPLOYEES and DEPARTMENTS.
A correlated subquery is a subquery that refers to a column from the outer query. The inner query depends on the current outer row, so Oracle evaluates the relationship for each outer row.
Comparing Each Employee with Their Department
Suppose you want employees whose salary is greater than the average salary in their own department. A single company-wide average is not enough. Each employee needs to be compared with a different department average.
SELECT e.employee_id,
e.last_name,
e.salary,
e.department_id
FROM employees e
WHERE e.salary > (
SELECT AVG(i.salary)
FROM employees i
WHERE i.department_id = e.department_id
);The outer query gives each employee the alias e. The inner query gives its copy of EMPLOYEES the alias i.
The condition i.department_id = e.department_id is the correlation. The reference to e.department_id comes from the current row of the outer query.
For an employee in department 20, the inner query calculates the average for department 20. For an employee in department 30, it calculates the average for department 30.
A logically equivalent join uses a grouped inline view:
SELECT e.employee_id,
e.last_name,
e.salary,
a.avg_salary
FROM employees e
JOIN (SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id) a
ON a.department_id = e.department_id
WHERE e.salary > a.avg_salary;The correlated version keeps the comparison close to the condition. The join version makes the intermediate department averages visible and may be easier to extend.
Correlation Scope and Aliases
Always use table aliases when a query reads the same table at more than one level. Without aliases, Oracle cannot reliably distinguish the inner and outer references.
This query counts employees in each department:
SELECT d.department_name,
(SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.department_id) AS headcount
FROM departments d;The scalar subquery returns one value for each department. The reference d.department_id makes it correlated.
A correlated subquery must return a valid result for every outer row. A scalar subquery must return at most one row. If it returns multiple rows, Oracle raises ORA-01427: single-row subquery returns more than one row.
Correlation does not necessarily mean Oracle physically runs the inner query once per row. The optimizer can transform, or “unnest,” a subquery into a join or another equivalent operation. Write the clearest correct query first, then inspect the execution plan when performance matters.
When to Use Correlation
Use a correlated subquery when the inner condition naturally depends on the current outer row:
- Compare a value with its group’s aggregate.
- Check whether a related row exists.
- Find rows with no matching child rows.
- Apply a per-row condition in an
UPDATEorDELETE.
A join or WITH query can be clearer when the intermediate result is reused, selected, or joined to several tables.
Exercise
What does the condition i.department_id = e.department_id do in the salary query?
Show answer
It connects the inner query to the current outer employee, so the inner aggregate uses that employee's department instead of calculating one average for the whole company.