1. Gyanastra
  2. Database Systems
  3. SQL Joins and Nested Queries
  4. ANY/SOME and ALL Operators with Comparison Operators (Subqueries)
Free lesson, no account needed 9 min read 11 practice questions after it
Asked in past papers7 times
  • GATE CSE20172015201220072005
  • UGC NET20132011

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 written SOME) and ALL are 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 as IN.
  • <>: the symbol formed by < and > together means "not equal to"; it can also be combined with ANY/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

  1. The inner (sub)query runs first and returns the list of values the outer query will be compared against.
  2. 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.
  3. > ALL against a set of salaries means the salary must exceed the highest value in that list; only such employees are printed.
  4. > 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.
  5. = ANY / = SOME prints an employee whose salary is exactly equal to one of the values returned by the inner query.
  6. For a single attribute, = ANY (...) ≡ = SOME (...) ≡ IN (...) in output.
  7. IN can compare more than one value (attribute) at the same time — for example, a project number and the number of hours compared simultaneously in one IN list.
    With = ANY or = SOME this is not possible: only one single attribute at a time can be compared.
  8. ANY / SOME / ALL can be combined with any comparison operator: =, <, >, >=, <=, choosing whichever suits the requirement.
  9. <> ANY = "not equal to at least one value"; <> SOME = "not equal to at least one value"; <> ALL = "not equal to all", which is equivalent to NOT IN.
  10. 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 SS be the set of values returned by the inner subquery, and let vv be the value of a row of the outer query.

  • v > ALL (S) ⇔ v>MAX(S)v > \text{MAX}(S) → only values greater than the highest value of SS qualify.

  • v > ANY (S) ( = v > SOME (S) ) ⇔ v>MIN(S)v > \text{MIN}(S) → values greater than the lowest value of SS qualify.

  • v = ANY (S) ⇔ v = SOME (S) ⇔ v IN (S) → equality with at least one value of SS.

  • v <> ALL (S) ⇔ v NOT IN (S) → vv matches none of the values of SS.

  • v < ALL (S) ⇔ v<MIN(S)v < \text{MIN}(S)

  • v < ANY (S) ⇔ v<MAX(S)v < \text{MAX}(S)

  • v >= ALL (S) ⇔ v≥MAX(S)v \geq \text{MAX}(S)

  • v <= ANY (S) ⇔ v≤MAX(S)v \leq \text{MAX}(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

FormMeaningEquivalent toRestriction
> ALL (subquery)greater than every valuegreater than MAX of subquerysingle attribute
> ANY / > SOME (subquery)greater than at least one valuegreater than MIN of subquerysingle attribute
= ANY / = SOME (subquery)equal to at least one valueIN (subquery)single attribute only
IN (list/subquery)equal to any value in list= ANY / = SOMEcan compare multiple attributes at once
<> ALL (subquery)not equal to any valueNOT INsingle attribute only
NOT IN (list/subquery)value matches none of the listed values<> ALLcan 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

  • ANY and SOME are synonyms — one can always replace the other with no change in output.
  • > ALL behaves like "greater than the maximum" of the inner list; > ANY behaves like "greater than the minimum" — easier to satisfy, so more rows are returned.
  • = ANY ≡ = SOME ≡ IN for single-attribute comparisons; all three give the same output.
  • Only IN can compare more than one attribute (multiple values) at the same time; = ANY / = SOME cannot.
  • ANY, SOME, ALL can be combined with =, <, >, <=, >= (and <>) as the requirement demands.
  • <> means "not equal to"; combined as <> ANY, <> SOME, <> ALL.
  • NOT IN keeps 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 to v > (SELECT MAX(...)) and v >= ALL (S) to v >= MAX(S); v > ANY (S) is equivalent to v > (SELECT MIN(...)).
    When the subquery may be empty, > ALL is vacuously TRUE and > ANY is FALSE — a classic trap.
    Asked inGATE 2015GATE 2012GATE 2007
  • NOT IN / <> ALL returns UNKNOWN (row excluded) if the subquery result contains a NULL; NOT EXISTS is the NULL-safe way to express "greater than all / none matches".
    Asked inGATE 2007
  • A correlated subquery with NOT EXISTS (or COUNT) can express "greater than all/everyone" without ALL: WHERE NOT EXISTS (SELECT * FROM s WHERE s.dept=5 AND s.salary >= e.salary).
    Asked inGATE 2007
  • > ANY / > SOME means "greater than at least one" = "greater than the minimum", i.e. greater than SOME value;
    it does NOT mean "greater than all".
    A join with T.assets > S.assets (non-aggregated) is the same as > ANY.
    Asked inUGC NET 2013UGC NET 2011
  • = ANY and IN are equivalent for single-column subqueries; <> ALL and NOT IN are equivalent; <> ANY is NOT equivalent to NOT 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); > ALL would exclude the maximum itself.
    Asked inGATE 2015
Pages 1–2 of 2