Database Systems · SQL Joins and Nested Queries
ANY/SOME and ALL Operators with Comparison Operators (Subqueries)
Overview
Quantified comparison operators — ANY (also written SOME) and ALL — are used with a comparison operator in front of a subquery.
The inner query returns a set of values, and the outer query keeps a row only if its value satisfies the comparison against that set. The meaning of the comparison changes depending on whether ANY/SOME or ALL is used.
Key concepts
- Quantified comparison operators:
ANY(also writtenSOME) andALLare placed together with a comparison operator (=,<,>,<=,>=,<>) in front of a subquery (inner query).
The inner query produces a set of values; the outer query compares each row's value against that set. ANY/SOME: mean the same thing and are fully interchangeable. The comparison must hold against at least one value returned by the inner query.ALL: the comparison must hold against every value returned by the inner query.> ALL: the value must exceed the highest value in the inner list.> ANY/> SOME: it is enough that the value exceeds at least one value; since the smallest value is the easiest to beat, this becomes "greater than the lowest (minimum) value" of the list.= ANY/= SOME: the value must be exactly equal to one of the values returned by the inner query; this gives the same output asIN.<>: the symbol formed by<and>together means "not equal to"; it can also be combined withANY/SOME/ALL.NOT IN: a row appears in the output if its value does not match any of the values inside the brackets.
How it works
- The inner (sub)query runs first and returns the list of values the outer query will be compared against.
- The outer query is evaluated row by row; a row's value is kept in the output only if the quantified comparison against the inner list is satisfied.
> ALLagainst a set of salaries means the salary must exceed the highest value in that list; only such employees are printed.> ANY(or> SOME) means the salary must exceed at least one value; in practice this is "salary greater than the minimum value" of the list, and every employee whose salary exceeds that minimum is printed.= ANY/= SOMEprints an employee whose salary is exactly equal to one of the values returned by the inner query.- For a single attribute,
= ANY (...)≡= SOME (...)≡IN (...)in output. INcan compare more than one value (attribute) at the same time — for example, a project number and the number of hours compared simultaneously in oneINlist.
With= ANYor= SOMEthis is not possible: only one single attribute at a time can be compared.ANY/SOME/ALLcan be combined with any comparison operator:=,<,>,>=,<=, choosing whichever suits the requirement.<> ANY= "not equal to at least one value";<> SOME= "not equal to at least one value";<> ALL= "not equal to all", which is equivalent toNOT IN.- Any of these forms may be used in place of the other as long as only a single attribute is being compared (not more than one attribute).
Formulas
-
Let be the set of values returned by the inner subquery, and let be the value of a row of the outer query.
-
v > ALL (S)⇔ → only values greater than the highest value of qualify. -
v > ANY (S)( =v > SOME (S)) ⇔ → values greater than the lowest value of qualify. -
v = ANY (S)⇔v = SOME (S)⇔v IN (S)→ equality with at least one value of . -
v <> ALL (S)⇔v NOT IN (S)→ matches none of the values of . -
v < ALL (S)⇔ -
v < ANY (S)⇔ -
v >= ALL (S)⇔ -
v <= ANY (S)⇔
Worked examples
Example 1 — > ALL. Requirement: find the first names of all employees whose salary is greater than all employees in department number 5. The inner query collects the salaries of department 5 from the employee table where dept_no = 5.
SELECT first_name
FROM employee
WHERE salary > ALL (SELECT salary
FROM employee
WHERE dept_no = 5);
The inner query returns, say, 10,000, 20,000, 30,000. The outer query keeps only an employee whose salary is greater than all three numbers — i.e. greater than 30,000. Only those first names are printed: employees earning more than every department-5 employee.
Example 2 — > ANY on the same data. Same query with ANY (or SOME) instead of ALL:
SELECT first_name
FROM employee
WHERE salary > ANY (SELECT salary
FROM employee
WHERE dept_no = 5);
With the set {10,000, 20,000, 30,000}:
- Employee A with salary 11,000 → 11,000 > 10,000, so A is printed (under
> ALL, A would not be printed). - An employee with salary 31,000 → 31,000 > 10,000, so that employee is printed.
Every employee whose salary is greater than the minimum value (10,000) is printed.
Example 3 — > ALL versus > ANY in words. "Greater than ALL" ⇒ only salaries greater than the highest value are printed. "Greater than ANY" ⇒ salaries greater than the lowest value are printed. Employee A with 11,000 fails under ALL but passes under ANY.
Example 4 — = ANY / = SOME versus IN. Requirement: find the first names of all employees whose salary is equal to any employee in department number 5.
SELECT first_name
FROM employee
WHERE salary = ANY (SELECT salary
FROM employee
WHERE dept_no = 5);
The inner query returns all department-5 salaries. If employee B has salary 10,000, which matches one of those salaries, B is printed. The identical result is obtained with:
SELECT first_name
FROM employee
WHERE salary = SOME (SELECT salary
FROM employee
WHERE dept_no = 5);
and also with:
SELECT first_name
FROM employee
WHERE salary IN (SELECT salary
FROM employee
WHERE dept_no = 5);
Hence = ANY ≡ = SOME ≡ IN for a single attribute.
Example 5 — <> ALL / NOT IN. For the value set inside the brackets, NOT IN keeps a row only if its value does not match any value in that set. Standard SQL defines NOT IN as equivalent to <> ALL, not <> ANY.
With <> ANY almost every row qualifies (only a row whose value is the sole value in the list fails). Use <> ALL / NOT IN when the requirement is "matches none of them".
Comparison
| Form | Meaning | Equivalent to | Restriction |
|---|---|---|---|
> ALL (subquery) | greater than every value | greater than MAX of subquery | single attribute |
> ANY / > SOME (subquery) | greater than at least one value | greater than MIN of subquery | single attribute |
= ANY / = SOME (subquery) | equal to at least one value | IN (subquery) | single attribute only |
IN (list/subquery) | equal to any value in list | = ANY / = SOME | can compare multiple attributes at once |
<> ALL (subquery) | not equal to any value | NOT IN | single attribute only |
NOT IN (list/subquery) | value matches none of the listed values | <> ALL | can also handle multiple attributes |
Key contrast: IN supports comparing two values simultaneously (e.g. project number and number of hours in one IN), while = ANY and = SOME do not — they compare only one attribute at a time. This is the advantage of IN over = ANY / = SOME.
Revision summary
ANYandSOMEare synonyms — one can always replace the other with no change in output.> ALLbehaves like "greater than the maximum" of the inner list;> ANYbehaves like "greater than the minimum" — easier to satisfy, so more rows are returned.= ANY≡= SOME≡INfor single-attribute comparisons; all three give the same output.- Only
INcan compare more than one attribute (multiple values) at the same time;= ANY/= SOMEcannot. ANY,SOME,ALLcan be combined with=,<,>,<=,>=(and<>) as the requirement demands.<>means "not equal to"; combined as<> ANY,<> SOME,<> ALL.NOT INkeeps values that match none of the values in the bracket; it is equivalent to<> ALL, not<> ANY.- Any of these forms can be substituted for another provided only a single attribute is being compared.
Previous-year exam insights
v > ALL (S)is equivalent tov > (SELECT MAX(...))andv >= ALL (S)tov >= MAX(S);v > ANY (S)is equivalent tov > (SELECT MIN(...)).
When the subquery may be empty,> ALLis vacuously TRUE and> ANYis FALSE — a classic trap.
Asked inGATE 2015GATE 2012GATE 2007NOT IN/<> ALLreturns UNKNOWN (row excluded) if the subquery result contains a NULL;NOT EXISTSis the NULL-safe way to express "greater than all / none matches".
Asked inGATE 2007- A correlated subquery with
NOT EXISTS(orCOUNT) can express "greater than all/everyone" withoutALL:WHERE NOT EXISTS (SELECT * FROM s WHERE s.dept=5 AND s.salary >= e.salary).
Asked inGATE 2007 > ANY/> SOMEmeans "greater than at least one" = "greater than the minimum", i.e. greater than SOME value;
it does NOT mean "greater than all".
A join withT.assets > S.assets(non-aggregated) is the same as> ANY.
Asked inUGC NET 2013UGC NET 2011= ANYandINare equivalent for single-column subqueries;<> ALLandNOT INare equivalent;<> ANYis NOT equivalent toNOT IN.
Asked inGATE 2015UGC NET 2013- To find rows with the maximum value, use
>= ALL (SELECT col FROM t)or= (SELECT MAX(col) FROM t);> ALLwould exclude the maximum itself.
Asked inGATE 2015