Subquery Comparisons
Applies to: ✅ Data Cloud SQL ✅ Tableau Hyper API
This section describes the SQL-compliant subquery expressions. All of the expression forms documented in this section return Boolean results.
Subquery comparisons executes until at least one row is returned, not to completion. For this reason, avoid writing a subquery with side effects.
The argument of EXISTS is an arbitrary SELECT
statement, or subquery. The subquery is evaluated to determine if 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 act as constants during any one evaluation of the subquery.
A common coding convention is to write all EXISTS
expressions in the form EXISTS(SELECT 1 WHERE ...)
. Exceptions to this convention include subqueries that use INTERSECT or EXCEPT.
This example is similar to an inner join on col2
, but it produces at most one output row for each tab1
row, even if there are several matching tab2
rows.
The right-hand side is a parenthesized subquery, that returns exactly one column. The left-hand 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 if the subquery returns no rows.
If the left-hand expression returns NULL
, or if there are no equal right-hand values and at least one right-hand row returns NULL
, the result of the IN construct is NULL
.
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 if the subquery returns no rows. The result is false if an equal row is found.
If the left-hand expression returns NULL
, or if there are no equal right-hand values and at least one right-hand row returns NULL
, the result of the NOT IN construct is NULL
.
The right-hand side is a parenthesized subquery that returns exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given <operator>
, which returns 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 case if the subquery returns no rows.
SOME is a synonym for ANY. IN is equivalent to = ANY
.
If there are no successes and at least one right-hand row yields NULL
for the operator's result, the result of the ANY construct is NULL
.
The right-hand side is a parenthesized subquery, which returns exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given <operator>
, which returns a Boolean result. The result of ALL is true if all rows yield true, including the case if the subquery returns no rows. The result is false 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
.