The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference
by The PostgreSQL Global Development Group Paperback (6"x9"), 454 pages ISBN 9781906966041 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |

### 7.20.3 NOT IN

expressionNOT IN (subquery)

The right-hand side is a parenthesized
subquery, which must return exactly one column. The left-hand 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 case where the subquery returns no rows).
The result is “false” if any equal row is found.

Note that if the left-hand expression yields null, or if there are
no equal right-hand values and at least one right-hand 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_constructorNOT IN (subquery)

The left-hand side of this form of `NOT IN`

is a row constructor,
as described in section 2.2.12 Row Constructors.
The right-hand side is a parenthesized
subquery, which must return exactly as many columns as there are
expressions in the left-hand row. The left-hand expressions are
evaluated and compared row-wise to each row of the subquery result.
The result of `NOT IN`

is “true” if only unequal subquery rows
are found (including the 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 non-null and equal; the rows
are unequal if any corresponding members are non-null and unequal;
otherwise the result of that row comparison is unknown (null).
If all the per-row results are either unequal or null, with at least one
null, then the result of `NOT IN`

is null.

ISBN 9781906966041 | The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference | See the print edition |