Correlated and non-correlated subqueries

Different subqueries require different approaches for efficient processing by DB2®.

All subqueries can be classified into either two categories: correlated and non-correlated

Correlated subqueries

Correlated subqueries contain a reference to a table or column that is outside of the scope of the subquery.

Begin general-use programming interface information.

In the following query, for example, the correlation name X is a value from a table that is not listed in the FROM clause of the subquery. The inclusion of X illustrates that the subquery references the outer query block:

SELECT * FROM DSN8A10.EMP X
   WHERE  JOB = 'DESIGNER'
      AND  EXISTS (SELECT 1
                  FROM   DSN8A10.PROJ
                  WHERE  DEPTNO = X.WORKDEPT
                     AND  MAJPROJ = 'MA2100');
End general-use programming interface information.

Non-correlated subqueries

Non-correlated subqueries do not refer to any tables or columns that are outside of the scope of the subquery.

Begin general-use programming interface information.
The following example query refers only to tables are within the scope of the FROM clause.

SELECT * FROM DSN8A10.EMP
  WHERE  JOB = 'DESIGNER'
    AND  WORKDEPT IN (SELECT DEPTNO
                      FROM   DSN8A10.PROJ
                      WHERE  MAJPROJ = 'MA2100');
End general-use programming interface information.