The SQL ANY and ALL Operators
The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.
The SQL ANY Operators
The ANY operators:
- returns a boolean value as a result
- returns TRUE if ANY of the subquery values meet the condition
Example
You can click on above box to edit the code and run again.ANY Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY ( SELECT column_name FROM table_name WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).
The SQL ALL Operators
The ALL operators:
- returns a boolean value as a result
- returns TRUE if ALL of the subquery values meet the condition
- is used with SELECT, WHERE and HAVING statements
ALL Syntax With SELECT
SELECT ALL column_name(s)
FROM table_name
WHERE condition;
ALL Syntax With WHERE or HAVING
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL (
SELECT column_name
FROM table_name WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).
Demo Products table
This product table is used in examples:
Demo Category table
This category table is used in examples:
SQL ANY Example
The following SQL statement lists the Product_name if it finds ANY records in the category table has category_id equal to 6 (this will return TRUE because the id column has value of 6):
Example
SELECT Product_nameYou can click on above box to edit the code and run again.
FROM products
WHERE Product_id = ANY
( SELECT Product_id FROM category WHERE Category_id = 6) ;
Example
SELECT Product_name FROM products WHERE Product_id = ANY ( SELECT Product_id FROM category WHERE Category_id > 4) ;You can click on above box to edit the code and run again.
Example
SELECT Product_name FROM products WHERE Product_id = ANY ( SELECT Product_id FROM category WHERE Category_id > 8) ;You can click on above box to edit the code and run again.
SQL ALL Example
The following SQL statement lists ALL the product names:
Example
SELECT ALL Product_nameYou can click on above box to edit the code and run again.
FROM products
WHERE TRUE;