

PostgreSQL 8.2.9 Documentation  

Prev  Fast Backward  Chapter 9. Functions and Operators  Fast Forward  Next 
This section describes the SQLcompliant subquery expressions available in PostgreSQL. All of the expression forms documented in this section return Boolean (true/false) results.
EXISTS (subquery)
The argument of EXISTS is an arbitrary SELECT statement, or subquery. The subquery is evaluated to determine whether it returns any rows. If it returns at least one row, the result of EXISTS is "true"; if the subquery returns no rows, the result of EXISTS is "false".
The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery.
The subquery will generally only be executed far enough to determine whether at least one row is returned, not all the way to completion. It is unwise to write a subquery that has any side effects (such as calling sequence functions); whether the side effects occur or not may be difficult to predict.
Since the result depends only on whether any rows are returned, and not on the contents of those rows, the output list of the subquery is normally uninteresting. A common coding convention is to write all EXISTS tests in the form EXISTS(SELECT 1 WHERE ...). There are exceptions to this rule however, such as subqueries that use INTERSECT.
This simple example is like an inner join on col2, but it produces at most one output row for each tab1 row, even if there are multiple matching tab2 rows:
SELECT col1 FROM tab1 WHERE EXISTS(SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
expression IN (subquery)
The righthand side is a parenthesized subquery, which must return exactly one column. The lefthand expression is evaluated and compared to each row of the subquery result. The result of IN is "true" if any equal subquery row is found. The result is "false" if no equal row is found (including the special case where the subquery returns no rows).
Note that if the lefthand expression yields null, or if there are no equal righthand values and at least one righthand row yields null, the result of the IN construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values.
As with EXISTS, it's unwise to assume that the subquery will be evaluated completely.
row_constructor IN (subquery)
The lefthand side of this form of IN is a row constructor, as described in Section 4.2.11. The righthand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the lefthand row. The lefthand expressions are evaluated and compared rowwise to each row of the subquery result. The result of IN is "true" if any equal subquery row is found. The result is "false" if no equal row is found (including the special case where the subquery returns no rows).
As usual, null values in the rows are combined per the normal rules of SQL Boolean expressions. Two rows are considered equal if all their corresponding members are nonnull and equal; the rows are unequal if any corresponding members are nonnull and unequal; otherwise the result of that row comparison is unknown (null). If all the perrow results are either unequal or null, with at least one null, then the result of IN is null.
expression NOT IN (subquery)
The righthand side is a parenthesized subquery, which must return exactly one column. The lefthand expression is evaluated and compared to each row of the subquery result. The result of NOT IN is "true" if only unequal subquery rows are found (including the special case where the subquery returns no rows). The result is "false" if any equal row is found.
Note that if the lefthand expression yields null, or if there are no equal righthand values and at least one righthand row yields null, the result of the NOT IN construct will be null, not true. This is in accordance with SQL's normal rules for Boolean combinations of null values.
As with EXISTS, it's unwise to assume that the subquery will be evaluated completely.
row_constructor NOT IN (subquery)
The lefthand side of this form of NOT IN is a row constructor, as described in Section 4.2.11. The righthand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the lefthand row. The lefthand expressions are evaluated and compared rowwise to each row of the subquery result. The result of NOT IN is "true" if only unequal subquery rows are found (including the special case where the subquery returns no rows). The result is "false" if any equal row is found.
As usual, null values in the rows are combined per the normal rules of SQL Boolean expressions. Two rows are considered equal if all their corresponding members are nonnull and equal; the rows are unequal if any corresponding members are nonnull and unequal; otherwise the result of that row comparison is unknown (null). If all the perrow results are either unequal or null, with at least one null, then the result of NOT IN is null.
expression operator ANY (subquery) expression operator SOME (subquery)
The righthand side is a parenthesized subquery, which must return exactly one column. The lefthand expression is evaluated and compared to each row of the subquery result using the given operator, which must yield a Boolean result. The result of ANY is "true" if any true result is obtained. The result is "false" if no true result is found (including the special case where the subquery returns no rows).
SOME is a synonym for ANY. IN is equivalent to = ANY.
Note that if there are no successes and at least one righthand row yields null for the operator's result, the result of the ANY construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values.
As with EXISTS, it's unwise to assume that the subquery will be evaluated completely.
row_constructor operator ANY (subquery) row_constructor operator SOME (subquery)
The lefthand side of this form of ANY is a row constructor, as described in Section 4.2.11. The righthand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the lefthand row. The lefthand expressions are evaluated and compared rowwise to each row of the subquery result, using the given operator. The result of ANY is "true" if the comparison returns true for any subquery row. The result is "false" if the comparison returns false for every subquery row (including the special case where the subquery returns no rows). The result is NULL if the comparison does not return true for any row, and it returns NULL for at least one row.
See Section 9.17.5 for details about the meaning of a rowwise comparison.
expression operator ALL (subquery)
The righthand side is a parenthesized subquery, which must return exactly one column. The lefthand expression is evaluated and compared to each row of the subquery result using the given operator, which must yield a Boolean result. The result of ALL is "true" if all rows yield true (including the special case where the subquery returns no rows). The result is "false" if any false result is found. The result is NULL if the comparison does not return false for any row, and it returns NULL for at least one row.
NOT IN is equivalent to <> ALL.
As with EXISTS, it's unwise to assume that the subquery will be evaluated completely.
row_constructor operator ALL (subquery)
The lefthand side of this form of ALL is a row constructor, as described in Section 4.2.11. The righthand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the lefthand row. The lefthand expressions are evaluated and compared rowwise to each row of the subquery result, using the given operator. The result of ALL is "true" if the comparison returns true for all subquery rows (including the special case where the subquery returns no rows). The result is "false" if the comparison returns false for any subquery row. The result is NULL if the comparison does not return false for any subquery row, and it returns NULL for at least one row.
See Section 9.17.5 for details about the meaning of a rowwise comparison.
row_constructor operator (subquery)
The lefthand side is a row constructor, as described in Section 4.2.11. The righthand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the lefthand row. Furthermore, the subquery cannot return more than one row. (If it returns zero rows, the result is taken to be null.) The lefthand side is evaluated and compared rowwise to the single subquery result row.
See Section 9.17.5 for details about the meaning of a rowwise comparison.