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
Wait — don't leave
without your free
AI/ML Roadmap

The step-by-step path used by 25,000+ learners to go from zero to career-ready in AI/ML.

Please fill in your name and a valid email.
🔒 100% Free ☕ No spam, ever ✓ Instant delivery

Roadmap sent to your inbox!

Can't find it? Check your spam or promotions tab.

Not sure where to start?

Book a free 15-min call — our team will map out the right path for your background. Zero sales pressure.

📞

Request a free callback

Team available · 15 min · No commitment

🇮🇳 +91
🇮🇳 India +91
🇺🇸 USA +1
🇬🇧 UK +44
🇦🇺 AUS +61
🇦🇪 UAE +971
🇸🇬 SG +65
🇲🇾 MY +60
🇵🇰 PK +92
🇧🇩 BD +880
🇳🇵 NP +977
🇱🇰 LK +94
🇫🇷 FR +33
🇩🇪 DE +49
🇮🇹 IT +39
🇪🇸 ES +34
🇳🇱 NL +31
🇸🇪 SE +46
🇨🇭 CH +41
🇵🇱 PL +48
🇹🇷 TR +90
🇸🇦 SA +966
🇶🇦 QA +974
🇰🇼 KW +965
🇴🇲 OM +968
🇧🇭 BH +973
🇪🇬 EG +20
🇿🇦 ZA +27
🇳🇬 NG +234
🇰🇪 KE +254
🇬🇭 GH +233
🇨🇳 CN +86
🇯🇵 JP +81
🇰🇷 KR +82
🇮🇩 ID +62
🇵🇭 PH +63
🇧🇷 BR +55
🇲🇽 MX +52
🇦🇷 AR +54
🇨🇦 CA +1
🇳🇿 NZ +64
🇮🇪 IE +353
Please enter your phone number.

Thank you for your submission!

Our team will call you shortly. You'll also receive a confirmation on your email.

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