Subqueries

Subqueries select data from a table. The data is then used to test a condition in the WHERE clause of the main query. For example, this query produces a list of employees who work in the Eastern division:

   SELECT NAME, ID
   FROM Q.STAFF
   WHERE DEPT = SOME
     (SELECT DEPTNUMB            +
     FROM Q.ORG                 |  subquery
     WHERE DIVISION='EASTERN')   +

First, the subquery finds the department numbers in the Eastern division. Then, the main query finds employees who work in any of these departments.

When there are several subqueries, the last one is executed first; the first one is executed last.

[ Previous Page | Next Page | Contents | Index ]