Menu

SQL ANY, ALL Operators – A Comprehensive Guide

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
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
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