ANY and ALL
ANY and ALL are logical operators that let you make comparisons on subqueries that return one or more rows. Both operators must be preceded by a comparison operator and followed by a subquery:
expression comparison-operator { ANY | ALL } (subquery)
- ANY returns true if the comparison between
expression
and any value returned bysubquery
evaluates to true. - ALL returns true only if the comparison between
expression
and all values returned bysubquery
evaluates to true.
Equivalent operators
You can use the following operators instead of ANY or ALL:
This operator... | Is equivalent to: |
---|---|
SOME | ANY |
IN | = ANY |
NOT IN | <> ALL |
NULL handling
Vertica supports multicolumn <> ALL subqueries where the columns are not marked NOT NULL
. If any column contains a NULL value, Vertica returns a run-time error.
Vertica does not support ANY subqueries that are nested in another expression if any column values are NULL.
Examples
Examples below use the following tables and data:
ANY subqueries
Subqueries that use the ANY keyword return true when any value retrieved in the subquery matches the value of the left-hand expression.
ANY subquery within an expression:
ANY noncorrelated subqueries without aggregates:
ANY noncorrelated subqueries with aggregates:
ANY noncorrelated subqueries with aggregates and a GROUP BY clause:
ANY noncorrelated subqueries with a GROUP BY clause:
ANY correlated subqueries with no aggregates or GROUP BY clause:
ALL subqueries
A subquery that uses the ALL keyword returns true when all values retrieved by the subquery match the left-hand expression, otherwise it returns false.
ALL noncorrelated subqueries without aggregates:
ALL noncorrelated subqueries with aggregates:
ALL noncorrelated subqueries with aggregates and a GROUP BY clause:
ALL noncorrelated subqueries with a GROUP BY clause: