<comparison> operator combined with an ANY or ALL quantifier. The subquery must return a single column.
ANYreturnsTRUEif the comparison is true for at least one row returned by the subquery.ALLreturnsTRUEif the comparison is true for every row returned by the subquery.
Syntax
Parameters
| Parameter | Description | Supported input types |
|---|---|---|
<value> | A scalar value to compare against each row of the subquery result. | Any comparable type |
<comparison> | A comparison operator: =, <>, !=, <, >, <=, or >=. | See Comparison operators |
<subquery> | A parenthesized SELECT statement that returns exactly one column. The subquery may be correlated with the outer query. | Any query that projects a single column of a comparable type |
<value> and the column type returned by <subquery> must be comparable.
Return type
BOOLEAN
Relationship to IN and NOT IN
Quantified comparison subqueries generalize IN and NOT IN:
<value> = ANY(<subquery>)is equivalent to<value> IN (<subquery>).<value> <> ALL(<subquery>)is equivalent to<value> NOT IN (<subquery>).
ANY and ALL when you need a comparison other than equality (for example, <, >=, or <>) against the rows of a subquery.
NULL handling
The result follows SQL three-valued logic:- If the subquery returns no rows,
ANYreturnsFALSEandALLreturnsTRUE. - For
ANY: returnsTRUEas soon as a matching row is found, even if other rows areNULL. If no row matches and at least one row isNULL(or the comparison itself isNULL), the result isNULL. Otherwise the result isFALSE. - For
ALL: returnsFALSEas soon as a non-matching row is found, even if other rows areNULL. If every row matches and at least one row isNULL(or the comparison itself isNULL), the result isNULL. Otherwise the result isTRUE.
Examples
The examples below use the following tables.Equality with ANY (equivalent to IN)
Find employees whose department_id matches any department located at location_id = 1700.
| name |
|---|
| Steven |
| Neena |
| Lex |
Non-equality with ALL (equivalent to NOT IN)
Find employees whose department_id does not match any department located at location_id = 1700.
| name |
|---|
| Alex |
| Bruce |
| David |
| Valli |
Range comparison with ALL
Find employees whose salary is greater than the highest salary in department 60. Because > ALL is true only when the value exceeds every row, this is equivalent to comparing against the maximum.
| name | salary |
|---|---|
| Steven | 24000.00 |
| Neena | 17000.00 |
| Lex | 17000.00 |
Range comparison with ANY
Find employees whose salary is greater than at least one salary in department 90. Because > ANY is true as soon as the value exceeds a single row, this is equivalent to comparing against the minimum.
| name | salary |
|---|---|
| Steven | 24000.00 |
Correlated subquery
The subquery can reference columns from the outer query. The following query finds, for each employee, whether their salary is greater than or equal to every other salary in the same department — that is, the top earner(s) per department.| name | department_id | salary |
|---|---|---|
| Alex | 60 | 9000.00 |
| Steven | 90 | 24000.00 |
Empty subquery result
When the subquery returns no rows,ANY is FALSE and ALL is vacuously TRUE.
FALSE
TRUE
NULL in the subquery result
If the subquery yields a NULL, the result follows three-valued logic. A definite match short-circuits the NULL:
TRUE — the match on 1 is found before the NULL matters.
NULL — no match is found, but a NULL prevents a definitive FALSE.
NULL — every non-NULL row differs from 1, but the NULL prevents a definitive TRUE.
See also
ANY/ALL(array) — quantified comparison against the elements of an array.- Subquery operators —
EXISTS,NOT EXISTS,IN,NOT IN.