SQL ANY, ALL Get Certified
Learn how the ANY and ALL operators compare a single value against a whole set of values returned by a subquery.
What are ANY and ALL?
In standard SQL the operators work as follows:
- ANY – the comparison is true for at least one value in the subquery result.
- ALL – the comparison is true for every value in the subquery result.
Important note for this tutorial: SQLite does not implement the ANY and ALL operators.
All executable examples below therefore use fully equivalent SQLite-compatible forms:
> ANY→EXISTS (... WHERE outer > inner)< ANY→EXISTS (... WHERE outer < inner)> ALL→outer > (SELECT MAX(...))< ALL→outer < (SELECT MIN(...))
Basic ANY Concept (> ANY)
“Greater than ANY” means the value is greater than at least one of the values returned by the subquery.
What this code does: Returns products whose price is higher than at least one of the special prices (1000, 1500, 2000). Monitor, Headphones and Webcam qualify; Keyboard does not.
WITH products(product_id, product_name, price) AS (
VALUES
(1, 'Keyboard', 800),
(2, 'Monitor', 2500),
(3, 'Headphones', 1200),
(4, 'Webcam', 1800)
),
special_prices(price) AS (
VALUES
(1000),
(1500),
(2000)
)
SELECT product_name, price
FROM products
WHERE EXISTS (
SELECT 1
FROM special_prices
WHERE products.price > special_prices.price
)
ORDER BY price;
ANY with Greater Than (Salary Example)
The same pattern applied to employee salaries.
What this code does: Returns employees whose salary is higher than at least one of the target salaries. Meera, Kabir and Riya qualify; Aarav does not.
WITH employees(employee_id, employee_name, salary) AS (
VALUES
(1, 'Aarav', 45000),
(2, 'Meera', 62000),
(3, 'Kabir', 75000),
(4, 'Riya', 90000)
),
salary_targets(salary) AS (
VALUES
(50000),
(70000),
(85000)
)
SELECT employee_name, salary
FROM employees
WHERE EXISTS (
SELECT 1
FROM salary_targets
WHERE employees.salary > salary_targets.salary
)
ORDER BY salary;
ANY with Less Than (< ANY)
“Less than ANY” means the value is lower than at least one of the values in the subquery.
What this code does: Returns car models whose price is less than at least one of the price limits. Model A, Model B and Model C qualify; Model D does not.
WITH cars(car_id, model, price) AS (
VALUES
(1, 'Model A', 18000),
(2, 'Model B', 25000),
(3, 'Model C', 32000),
(4, 'Model D', 45000)
),
price_limits(price) AS (
VALUES
(20000),
(30000),
(40000)
)
SELECT model, price
FROM cars
WHERE EXISTS (
SELECT 1
FROM price_limits
WHERE cars.price < price_limits.price
)
ORDER BY price;
Basic ALL Concept (> ALL)
“Greater than ALL” means the value is greater than every value returned by the subquery. The cleanest SQLite equivalent is to compare against the maximum of that set.
What this code does: Returns employees whose salary is higher than every target salary (i.e. higher than the highest target of 80 000). Only Tara and Dev qualify.
WITH employees(employee_id, employee_name, salary) AS (
VALUES
(1, 'Nisha', 55000),
(2, 'Rohan', 72000),
(3, 'Tara', 95000),
(4, 'Dev', 110000)
),
salary_targets(salary) AS (
VALUES
(60000),
(70000),
(80000)
)
SELECT employee_name, salary
FROM employees
WHERE salary > (
SELECT MAX(salary)
FROM salary_targets
)
ORDER BY salary;
ALL with Less Than (< ALL)
“Less than ALL” means the value is lower than every value in the subquery. The SQLite equivalent compares against the minimum of that set.
What this code does: Returns products whose price is lower than every minimum-price threshold (i.e. lower than the lowest threshold of 15 000). Only Tablet A qualifies.
WITH products(product_id, product_name, price) AS (
VALUES
(1, 'Tablet A', 9000),
(2, 'Tablet B', 14000),
(3, 'Tablet C', 18000),
(4, 'Tablet D', 25000)
),
minimum_prices(price) AS (
VALUES
(15000),
(20000),
(25000)
)
SELECT product_name, price
FROM products
WHERE price < (
SELECT MIN(price)
FROM minimum_prices
)
ORDER BY price;
ANY / ALL Equivalence Table
Equality with ANY (Membership Test)
= ANY is conceptually the same as IN. The SQLite form uses EXISTS.
What this code does: Returns students whose score appears in the list of top scores. Priya, Rahul and Simran qualify.
WITH students(student_id, student_name, score) AS (
VALUES
(1, 'Aman', 78),
(2, 'Priya', 91),
(3, 'Rahul', 85),
(4, 'Simran', 96)
),
top_scores(score) AS (
VALUES
(85),
(91),
(96)
)
SELECT student_name, score
FROM students
WHERE EXISTS (
SELECT 1
FROM top_scores
WHERE students.score = top_scores.score
)
ORDER BY score DESC;
ALL Expressed with NOT EXISTS
Another clean way to write “greater than ALL” is to assert that no value in the subquery is greater than or equal to the outer value.
What this code does: Returns players whose score is higher than every required score. Only Sahil (110) qualifies.
WITH players(player_id, player_name, score) AS (
VALUES
(1, 'Arjun', 70),
(2, 'Kiran', 82),
(3, 'Vivek', 95),
(4, 'Sahil', 110)
),
required_scores(score) AS (
VALUES
(80),
(90),
(100)
)
SELECT player_name, score
FROM players AS p
WHERE NOT EXISTS (
SELECT 1
FROM required_scores AS r
WHERE r.score >= p.score
)
ORDER BY score;
ALL with Aggregate Functions
Using MAX (or MIN) remains the most readable way to express ALL comparisons in SQLite.
What this code does: Returns candidates whose score is higher than every benchmark score. Only Mohan (105) qualifies.
WITH candidates(candidate_id, candidate_name, score) AS (
VALUES
(1, 'Neha', 72),
(2, 'Ravi', 88),
(3, 'Tanya', 94),
(4, 'Mohan', 105)
),
benchmark_scores(score) AS (
VALUES
(75),
(85),
(95)
)
SELECT candidate_name, score
FROM candidates
WHERE score > (
SELECT MAX(score)
FROM benchmark_scores
)
ORDER BY score DESC;
Common Uses of ANY and ALL
- Compare a single value against a whole set of values from another table or subquery.
- Find rows that exceed at least one threshold (ANY) or every threshold (ALL).
- Filter prices, salaries, scores, quantities, etc.
- Write portable SQLite code by translating ANY/ALL into EXISTS, MAX or MIN.
Best Practices
- Remember that SQLite does not support ANY / ALL directly.
- Prefer the MAX / MIN form for ALL comparisons – it is short and clear.
- Prefer EXISTS for ANY comparisons.
- Use NOT EXISTS when it expresses the “greater than ALL” logic more naturally.
- Always give clear aliases so correlated subqueries stay readable.
Although SQLite lacks the native ANY and ALL operators,
their logic is easy to reproduce with EXISTS, NOT EXISTS, MAX() and MIN().
Click Test My Code on any example to run the SQLite-compatible version live, or
Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click "Run Code" to see the result on the right.
Click "Run Code" to see the result here.