CorSubQueryExample(
CorSubQueryExample(
Suppose that you want a list of all the employees whose education levels are higher than the
average education levels in their respective departments. To get this information, SQL must
search the CORPDATA.EMPLOYEE table. For each employee in the table, SQL needs to
compare the employee's education level to the average education level for the employee's
department. In the subquery, you tell SQL to calculate the average education level for the
department number in the current row. For example:
A correlated subquery looks like an uncorrelated one, except for the presence of one or more
correlated references. In the example, the single correlated reference is the occurrence of
X.WORKDEPT in the subselect's FROM clause. Here, the qualifier X is the correlation name
defined in the FROM clause of the outer SELECT statement. In that clause, X is introduced as
the correlation name of the table CORPDATA.EMPLOYEE.
Now, consider what happens when the subquery is executed for a given row of
CORPDATA.EMPLOYEE. Before it is executed, the occurrence of X.WORKDEPT is replaced
with the value of the WORKDEPT column for that row. Suppose, for example, that the row is
for CHRISTINE I HAAS. Her work department is A00, which is the value of WORKDEPT for
this row. The subquery executed for this row is:
(SELECT AVG(EDLEVEL)
FROM CORPDATA.EMPLOYEE
WHERE WORKDEPT = 'A00')
Thus, for the row considered, the subquery produces the average education level of Christine's
department. This is then compared in the outer statement to Christine's own education level. For
some other row for which WORKDEPT has a different value, that value appears in the subquery
in place of A00. For example, for the row for MICHAEL L THOMPSON, this value is B01, and
the subquery for his row delivers the average education level for department B01.
The result table produced by the query has the following values: