2 9.24. Subquery Expressions #
9 9.24.6. Single-Row Comparison
11 This section describes the SQL-compliant subquery expressions available
12 in PostgreSQL. All of the expression forms documented in this section
13 return Boolean (true/false) results.
19 The argument of EXISTS is an arbitrary SELECT statement, or subquery.
20 The subquery is evaluated to determine whether it returns any rows. If
21 it returns at least one row, the result of EXISTS is “true”; if the
22 subquery returns no rows, the result of EXISTS is “false”.
24 The subquery can refer to variables from the surrounding query, which
25 will act as constants during any one evaluation of the subquery.
27 The subquery will generally only be executed long enough to determine
28 whether at least one row is returned, not all the way to completion. It
29 is unwise to write a subquery that has side effects (such as calling
30 sequence functions); whether the side effects occur might be
33 Since the result depends only on whether any rows are returned, and not
34 on the contents of those rows, the output list of the subquery is
35 normally unimportant. A common coding convention is to write all EXISTS
36 tests in the form EXISTS(SELECT 1 WHERE ...). There are exceptions to
37 this rule however, such as subqueries that use INTERSECT.
39 This simple example is like an inner join on col2, but it produces at
40 most one output row for each tab1 row, even if there are several
44 WHERE EXISTS (SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
48 expression IN (subquery)
50 The right-hand side is a parenthesized subquery, which must return
51 exactly one column. The left-hand expression is evaluated and compared
52 to each row of the subquery result. The result of IN is “true” if any
53 equal subquery row is found. The result is “false” if no equal row is
54 found (including the case where the subquery returns no rows).
56 Note that if the left-hand expression yields null, or if there are no
57 equal right-hand values and at least one right-hand row yields null,
58 the result of the IN construct will be null, not false. This is in
59 accordance with SQL's normal rules for Boolean combinations of null
62 As with EXISTS, it's unwise to assume that the subquery will be
64 row_constructor IN (subquery)
66 The left-hand side of this form of IN is a row constructor, as
67 described in Section 4.2.13. The right-hand side is a parenthesized
68 subquery, which must return exactly as many columns as there are
69 expressions in the left-hand row. The left-hand expressions are
70 evaluated and compared row-wise to each row of the subquery result. The
71 result of IN is “true” if any equal subquery row is found. The result
72 is “false” if no equal row is found (including the case where the
73 subquery returns no rows).
75 As usual, null values in the rows are combined per the normal rules of
76 SQL Boolean expressions. Two rows are considered equal if all their
77 corresponding members are non-null and equal; the rows are unequal if
78 any corresponding members are non-null and unequal; otherwise the
79 result of that row comparison is unknown (null). If all the per-row
80 results are either unequal or null, with at least one null, then the
85 expression NOT IN (subquery)
87 The right-hand side is a parenthesized subquery, which must return
88 exactly one column. The left-hand expression is evaluated and compared
89 to each row of the subquery result. The result of NOT IN is “true” if
90 only unequal subquery rows are found (including the case where the
91 subquery returns no rows). The result is “false” if any equal row is
94 Note that if the left-hand expression yields null, or if there are no
95 equal right-hand values and at least one right-hand row yields null,
96 the result of the NOT IN construct will be null, not true. This is in
97 accordance with SQL's normal rules for Boolean combinations of null
100 As with EXISTS, it's unwise to assume that the subquery will be
101 evaluated completely.
102 row_constructor NOT IN (subquery)
104 The left-hand side of this form of NOT IN is a row constructor, as
105 described in Section 4.2.13. The right-hand side is a parenthesized
106 subquery, which must return exactly as many columns as there are
107 expressions in the left-hand row. The left-hand expressions are
108 evaluated and compared row-wise to each row of the subquery result. The
109 result of NOT IN is “true” if only unequal subquery rows are found
110 (including the case where the subquery returns no rows). The result is
111 “false” if any equal row is found.
113 As usual, null values in the rows are combined per the normal rules of
114 SQL Boolean expressions. Two rows are considered equal if all their
115 corresponding members are non-null and equal; the rows are unequal if
116 any corresponding members are non-null and unequal; otherwise the
117 result of that row comparison is unknown (null). If all the per-row
118 results are either unequal or null, with at least one null, then the
119 result of NOT IN is null.
123 expression operator ANY (subquery)
124 expression operator SOME (subquery)
126 The right-hand side is a parenthesized subquery, which must return
127 exactly one column. The left-hand expression is evaluated and compared
128 to each row of the subquery result using the given operator, which must
129 yield a Boolean result. The result of ANY is “true” if any true result
130 is obtained. The result is “false” if no true result is found
131 (including the case where the subquery returns no rows).
133 SOME is a synonym for ANY. IN is equivalent to = ANY.
135 Note that if there are no successes and at least one right-hand row
136 yields null for the operator's result, the result of the ANY construct
137 will be null, not false. This is in accordance with SQL's normal rules
138 for Boolean combinations of null values.
140 As with EXISTS, it's unwise to assume that the subquery will be
141 evaluated completely.
142 row_constructor operator ANY (subquery)
143 row_constructor operator SOME (subquery)
145 The left-hand side of this form of ANY is a row constructor, as
146 described in Section 4.2.13. The right-hand side is a parenthesized
147 subquery, which must return exactly as many columns as there are
148 expressions in the left-hand row. The left-hand expressions are
149 evaluated and compared row-wise to each row of the subquery result,
150 using the given operator. The result of ANY is “true” if the comparison
151 returns true for any subquery row. The result is “false” if the
152 comparison returns false for every subquery row (including the case
153 where the subquery returns no rows). The result is NULL if no
154 comparison with a subquery row returns true, and at least one
155 comparison returns NULL.
157 See Section 9.25.5 for details about the meaning of a row constructor
162 expression operator ALL (subquery)
164 The right-hand side is a parenthesized subquery, which must return
165 exactly one column. The left-hand expression is evaluated and compared
166 to each row of the subquery result using the given operator, which must
167 yield a Boolean result. The result of ALL is “true” if all rows yield
168 true (including the case where the subquery returns no rows). The
169 result is “false” if any false result is found. The result is NULL if
170 no comparison with a subquery row returns false, and at least one
171 comparison returns NULL.
173 NOT IN is equivalent to <> ALL.
175 As with EXISTS, it's unwise to assume that the subquery will be
176 evaluated completely.
177 row_constructor operator ALL (subquery)
179 The left-hand side of this form of ALL is a row constructor, as
180 described in Section 4.2.13. The right-hand side is a parenthesized
181 subquery, which must return exactly as many columns as there are
182 expressions in the left-hand row. The left-hand expressions are
183 evaluated and compared row-wise to each row of the subquery result,
184 using the given operator. The result of ALL is “true” if the comparison
185 returns true for all subquery rows (including the case where the
186 subquery returns no rows). The result is “false” if the comparison
187 returns false for any subquery row. The result is NULL if no comparison
188 with a subquery row returns false, and at least one comparison returns
191 See Section 9.25.5 for details about the meaning of a row constructor
194 9.24.6. Single-Row Comparison #
196 row_constructor operator (subquery)
198 The left-hand side is a row constructor, as described in
199 Section 4.2.13. The right-hand side is a parenthesized subquery, which
200 must return exactly as many columns as there are expressions in the
201 left-hand row. Furthermore, the subquery cannot return more than one
202 row. (If it returns zero rows, the result is taken to be null.) The
203 left-hand side is evaluated and compared row-wise to the single
206 See Section 9.25.5 for details about the meaning of a row constructor