Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.24. Subquery Expressions
Prev UpChapter 9. Functions and OperatorsHome Next

9.24. Subquery Expressions#

This section describes theSQL-compliant subquery expressions available inPostgres Pro. All of the expression forms documented in this section return Boolean (true/false) results.

EXISTS (subquery)

The argument ofEXISTS is an arbitrarySELECT statement, orsubquery. The subquery is evaluated to determine whether it returns any rows. If it returns at least one row, the result ofEXISTS istrue; if the subquery returns no rows, the result ofEXISTS isfalse.

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 long 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 side effects (such as calling sequence functions); whether the side effects occur might be unpredictable.

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 unimportant. A common coding convention is to write allEXISTS tests in the formEXISTS(SELECT 1 WHERE ...). There are exceptions to this rule however, such as subqueries that useINTERSECT.

This simple example is like an inner join oncol2, but it produces at most one output row for eachtab1 row, even if there are several matchingtab2 rows:

SELECT col1FROM tab1WHERE EXISTS (SELECT 1 FROM tab2 WHERE col2 = tab1.col2);

9.24.2. IN#

expression 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 ofIN istrue if any equal subquery row is found. The result isfalse if no equal row is found (including the case where the subquery returns no rows).

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 theIN construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values.

As withEXISTS, it's unwise to assume that the subquery will be evaluated completely.

row_constructor IN (subquery)

The left-hand side of this form ofIN is a row constructor, as described inSection 4.2.13. 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 ofIN istrue if any equal subquery row is found. The result isfalse if no equal row is found (including the 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 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 ofIN is null.

9.24.3. NOT IN#

expression NOT 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 ofNOT IN istrue if only unequal subquery rows are found (including the case where the subquery returns no rows). The result isfalse 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 theNOT IN construct will be null, not true. This is in accordance with SQL's normal rules for Boolean combinations of null values.

As withEXISTS, it's unwise to assume that the subquery will be evaluated completely.

row_constructor NOT IN (subquery)

The left-hand side of this form ofNOT IN is a row constructor, as described inSection 4.2.13. 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 ofNOT IN istrue if only unequal subquery rows are found (including the case where the subquery returns no rows). The result isfalse 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 ofNOT IN is null.

9.24.4. ANY/SOME#

expressionoperator ANY (subquery)expressionoperator SOME (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 using the givenoperator, which must yield a Boolean result. The result ofANY istrue if any true result is obtained. The result isfalse if no true result is found (including the case where the subquery returns no rows).

SOME is a synonym forANY.IN is equivalent to= ANY.

Note that if there are no successes and at least one right-hand row yields null for the operator's result, the result of theANY construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values.

As withEXISTS, it's unwise to assume that the subquery will be evaluated completely.

row_constructoroperator ANY (subquery)row_constructoroperator SOME (subquery)

The left-hand side of this form ofANY is a row constructor, as described inSection 4.2.13. 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, using the givenoperator. The result ofANY istrue if the comparison returns true for any subquery row. The result isfalse if the comparison returns false for every subquery row (including the case where the subquery returns no rows). The result is NULL if no comparison with a subquery row returns true, and at least one comparison returns NULL.

SeeSection 9.25.5 for details about the meaning of a row constructor comparison.

9.24.5. ALL#

expressionoperator ALL (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 using the givenoperator, which must yield a Boolean result. The result ofALL istrue if all rows yield true (including the case where the subquery returns no rows). The result isfalse if any false result is found. The result is NULL if no comparison with a subquery row returns false, and at least one comparison returns NULL.

NOT IN is equivalent to<> ALL.

As withEXISTS, it's unwise to assume that the subquery will be evaluated completely.

row_constructoroperator ALL (subquery)

The left-hand side of this form ofALL is a row constructor, as described inSection 4.2.13. 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, using the givenoperator. The result ofALL istrue if the comparison returns true for all subquery rows (including the case where the subquery returns no rows). The result isfalse if the comparison returns false for any subquery row. The result is NULL if no comparison with a subquery row returns false, and at least one comparison returns NULL.

SeeSection 9.25.5 for details about the meaning of a row constructor comparison.

9.24.6. Single-Row Comparison#

row_constructoroperator (subquery)

The left-hand side is a row constructor, as described inSection 4.2.13. The right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. Furthermore, the subquery cannot return more than one row. (If it returns zero rows, the result is taken to be null.) The left-hand side is evaluated and compared row-wise to the single subquery result row.

SeeSection 9.25.5 for details about the meaning of a row constructor comparison.


Prev Up Next
9.23. Merge Support Functions Home 9.25. Row and Array Comparisons
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp