Menu

SQL ANY, ALL Operators – A Comprehensive Guide

The SQL ANY and ALL operators are used with a WHERE or HAVING clause. They enable us to compare a value to any or all values in a subquery or a list. Let's dive into the nitty-gritty of these two operators.

Written by Jagdeesh | 2 min read

The SQL ANY and ALL operators are used with a WHERE or HAVING clause. They enable us to compare a value to any or all values in a subquery or a list. Let’s dive into the nitty-gritty of these two operators.

SQL ANY Operator

The SQL ANY operator compares a value to any value within a set. The ANY operator returns true if any of the subquery values meet the condition.

Syntax

sql
SELECT column1 [, column2 ]
FROM table_name
WHERE columnN [comparison_operator] ANY (SELECT column_name FROM table_name WHERE condition);

Here, comparison_operator is any standard SQL comparison operator like =, <>, !=, >, <, >=, <=.

Example

Consider a orders table with the following data:

output
| OrderId | Product | Quantity |
|---------|---------|----------|
| 1       | Apples  | 50       |
| 2       | Oranges | 100      |
| 3       | Bananas | 150      |
| 4       | Apples  | 200      |
| 5       | Oranges | 250      |

Let’s use the SQL ANY operator to find products that have any quantity greater than 100:

sql
SELECT Product 
FROM orders 
WHERE Quantity > ANY (SELECT Quantity FROM orders WHERE Quantity > 100);

The subquery (SELECT Quantity FROM orders WHERE Quantity > 100) returns 150 and 250. Hence, our main query will select the products which have quantity more than either 150 or 250.

This query would return:

output
| Product |
|---------|
| Bananas |
| Apples  |
| Oranges |

SQL ALL Operator

The SQL ALL operator compares a value to all values in another value set or the result of the subquery. It returns true if all of the subquery values meet the condition.

Syntax

sql
SELECT column1 [, column2 ]
FROM table_name
WHERE columnN [comparison_operator] ALL (SELECT column_name FROM table_name WHERE condition);

Here, comparison_operator is any standard SQL comparison operator.

Example

Let’s use the same orders table for this example.

Suppose ypu want to find out the products that have a quantity less than all the quantities in the orders table. You can use the SQL ALL operator as follows:

sql
SELECT Product 
FROM orders 
WHERE Quantity < ALL (SELECT Quantity FROM orders WHERE Quantity > 50);

The subquery (SELECT Quantity FROM orders WHERE Quantity > 50) returns 100, 150, 200 and 250. The main query, therefore, will select the products which have a quantity less than all the values returned by the subquery, which in this case will be “Apples” with a quantity of 50.

This query would return:

output
| Product |
|---------|
| Apples  |

Conclusion

Mastering SQL operators like ANY and ALL will allow you to write more complex and versatile queries. SQL is a powerful language that, when well-understood, can reveal valuable insights hidden in your data.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Machine Learning Plus
MachineLearningPlus Expert AI/ML Training

Get a Free 30-Min
Guidance Call

Let our ML expert call you back and guide you for free

You're all set!
Tired of tutorials that go nowhere? This video shows you why and what to do instead.
Watch the Free Training
Redirecting in 5 seconds...
No thanks, I'll figure it out myself

Get your Start in AI/ML with the Foundations for Data Science (AI/ML) Course

Enroll for Free
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science