Subqueries
A subquery is a query within another query.
The following operators for subqueries are supported:
ALLandANY[NOT] EXISTS[NOT] IN.
ALL / ANY
They allow a comparison operator to be applied to the values of the result set of a subquery.
Syntax
expr compOperator {ALL | ANY} (query)Parameters
compOperator (comparison operator): One of the following operators: =, !=, >, >=, <, <=.
Usage Notes
- expr is compared with each value of the result set from the subquery using the defined operator. Specifically:
- If
ANYis specified and at least one row of the subquery meets the condition, the result of the comparison isTRUE; otherwise,FALSEis returned. - If
ALLis specified and every row of the subquery meets the condition,TRUEis returned; otherwise,FALSE.
- If
ANYandALLsubqueries can only be used in aWHEREclause.ANYandALLsubqueries cannot be used with anORoperator.- Multiple entries in the
SELECTlist are not supported.
Example
Retrieve rows from the first object that do not have a corresponding row in the second object.
with
cte_2cols1 as ( select * from (select 'a' as ord, 10 as col1
union all
select 'b' as ord, 20 as col1
union all
select 'c' as ord, 30 as col1
union all
select 'd' as ord, 40 as col1
union all
select 'f' as ord, NULL as col2)),
cte_2cols2 as ( select * from (select 'a' as ord, 10 as col2
union all
select 'b' as ord, 20 as col2
union all
select 'c' as ord, 30 as col2
union all
select 'd' as ord, 40 as col2
union all
select 'e' as ord, 50 as col2))
select ord
from cte_2cols1 table1
where table1.ord != ALL (SELECT table2.ord
FROM cte_2cols2 table2); +-----+
| ord |
+-----+
| f |
+-----+[NOT] EXISTS
An EXISTS subquery is a boolean expression. It can be used in a WHERE clause, a HAVING clause, or in a function that operates with boolean expressions:
EXISTSexpression =TRUEif the subquery produces rows.NOT EXISTSexpression =TRUEif the subquery produces no rows.
Syntax
[NOT] EXISTS (query)Usage Notes
- Correlated
EXISTSsubqueries can only be used in aWHEREclause. - Correlated
EXISTSsubqueries cannot be used as an argument with anORoperator. - Non-correlated
EXISTSsubqueries are supported where a boolean expression is allowed.
Example
Retrieve rows from the first object that do not have a corresponding row in the second object.
with
cte_2cols1 as ( select * from (select 'a' as ord, 10 as col1
union all
select 'b' as ord, 20 as col1
union all
select 'c' as ord, 30 as col1
union all
select 'd' as ord, 40 as col1
union all
select 'f' as ord, NULL as col2)),
cte_2cols2 as ( select * from (select 'a' as ord, 10 as col2
union all
select 'b' as ord, 20 as col2
union all
select 'c' as ord, 30 as col2
union all
select 'd' as ord, 40 as col2
union all
select 'e' as ord, 50 as col2))
select ord
from cte_2cols1 table1
where not exists (select 1
from cte_2cols2 table2
where
table2.ord=table1.ord)
;+-----+
| ord |
+-----+
| f |
+-----+[NOT] IN
IN and NOT IN check whether an expression from the query is contained in the values of the subquery.
Syntax
expr [NOT] IN (query)Usage Notes
INcan be seen as a shorthand for= (equals) ANYand is subject to the same restrictions as otherANYsubqueries.NOT INcan be seen as a shorthand for!= (not equals) ALLand is subject to the same restrictions as otherALLsubqueries.It is possible to use
[NOT] INas an operator in expressions that do not contain subqueries. See also [NOT] IN.
Example
This query is equivalent to the example for the ALL operator above.
with
cte_2cols1 as ( select * from (select 'a' as ord, 10 as col1
union all
select 'b' as ord, 20 as col1
union all
select 'c' as ord, 30 as col1
union all
select 'd' as ord, 40 as col1
union all
select 'f' as ord, NULL as col2)),
cte_2cols2 as ( select * from (select 'a' as ord, 10 as col2
union all
select 'b' as ord, 20 as col2
union all
select 'c' as ord, 30 as col2
union all
select 'd' as ord, 40 as col2
union all
select 'e' as ord, 50 as col2))
select ord
from cte_2cols1 table1
where table1.ord NOT IN (SELECT table2.ord
FROM cte_2cols2 table2); +-----+
| ord |
+-----+
| f |
+-----+