MySQL ALL Operator
MySQL ALL Operator: Testing Against Every Subquery Value
The MySQL ALL operator compares a single column or expression against every value returned by a subquery. The condition returns true only if the comparison holds for every single row in that subquery.
Syntax
Pair ALL with a standard comparison operator (such as greater than, less than, equal, or not equal) immediately before a single-column subquery:
SELECT columns
FROM table_name
WHERE column_name > ALL (
SELECT target_column
FROM other_table
WHERE target_column IS NOT NULL
);
If the subquery returns zero rows, ALL evaluates to true for every outer row (vacuous truth in SQL).
Practical SQL Example
This query finds products in demo_products whose price is strictly higher than every item in the 'Accessories' category:
SELECT id, product_name, category, price
FROM demo_products
WHERE price > ALL (
SELECT price
FROM demo_products
WHERE category = 'Accessories'
AND price IS NOT NULL
)
ORDER BY price ASC;
| id | product_name | category | price |
|---|---|---|---|
| 8 | Wireless Headphones | Electronics | 89.99 |
| 1 | Mechanical Keyboard | Electronics | 129.99 |
Notice that comparing against ALL behaves much like comparing against MAX(price), except when the subquery is empty: comparing with ALL on an empty set returns every row, while comparing against MAX() returns NULL and drops every row.
Common Mistakes and Best Practices
- Filter
NULLInside the Subquery: If the subquery returns even oneNULLvalue,ALLcomparisons evaluate toUNKNOWNand drop every outer row. Always addWHERE target_column IS NOT NULL. - Prefer
MAX()orNOT EXISTSfor Readability: Most teams findMAX(price)orNOT EXISTSeasier to read and tune thanALLsubqueries.
Frequently Asked Questions
What is not-equal ALL equivalent to in MySQL?
Comparing with not-equal ALL means a value differs from every row in the subquery, which is logically identical to NOT IN (subquery).
Why did > ALL (subquery) return all rows in my table?
Your subquery returned zero rows, and SQL defines comparisons against an empty ALL set as true.
Can I pass a static list like > ALL (10, 20, 30) in MySQL?
No, ALL only accepts a subquery in MySQL; for static numbers, simply compare against the largest value (> 30).
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.