SQL – ANY, ALL Operators
Table of content
- The SQL ANY Operator
- ANY with ”>” Operator
- ANY with ”<” Operator
- ANY with ”=” Operator
- The SQL ALL Operator
- ALL with WHERE Statement
- ALL with HAVING Clause
”;
The SQL ANY and ALL operators are used to perform a comparison between a single value and a range of values returned by the subquery.
The ANY and ALL operators must be preceded by a standard comparison operator i.e. >, >=, <, <=, =, <>, != and followed by a subquery.
The main difference between ANY and ALL is that ANY returns true if any of the subquery values meet the condition whereas ALL returns true if all of the subquery values meet the condition.
The SQL ANY Operator
The ANY operator is used to verify if any single record of a query satisfies the required condition.
This operator returns a TRUE, if the given condition is satisfied for any of the values in the range. If none of the values in the specified range satisfy the given condition, this operator returns false. You can also use another query (subquery) along with this operator.
Syntax
The basic syntax of the SQL – ANY operator is as follows −
Column_name operator ANY (subquery);
Where,
-
column_name is the name of a column in the main query.
-
operator is a comparison operator such as =, <, >, <=, >=, or <>.
-
subquery is a SELECT statement that returns a single column of values.
ANY with ”>” Operator
Typically, the ANY operator is used to compare a value with a set of values returned by a subquery, in such cases we can use it with the > (greater than) operator to verify if a particular column value is greater than column value of any of the records returned by the sub query.
Example
To understand it better let us consider the CUSTOMERS table which contains the personal details of customers including their name, age, address and salary etc. as shown below −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 ), (3, ''Kaushik'', 23, ''Kota'', 2000.00 ), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ), (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ), (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ), (7, ''Muffy'', 24, ''Indore'', 10000.00 );
The table will be created as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Now, let us list out the details of all the CUSTOMERS whose SALARY is greater than the SALARY of any customer whose AGE is 32 i.e. Chaitali, Hardik, Komal and Muffy in this case −
SELECT * FROM CUSTOMERS WHERE SALARY > ANY (SELECT SALARY FROM CUSTOMERS WHERE AGE = 32);
Output
The result obtained is as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
ANY with ”<” Operator
Similar to the ”>” operator, we can use the ”<” (less than) operator along with ANY to verify if a particular column value is less than column value of any of the records returned by the sub query.
Example
In here, we are finding the distinct/different age of customers having any salary less than the average salary of all the customers from the CUSTOMERS table previously created −
SELECT DISTINCT AGE FROM CUSTOMERS WHERE SALARY < ANY (SELECT AVG(SALARY) FROM CUSTOMERS);
Output
We get the following output while executing the above query −
AGE |
---|
32 |
25 |
23 |
22 |
ANY with ”=” Operator
When we use the = (equal to) operator along with ANY, it verifies if a particular column value is equal to the column value of any of the records returned by the sub query.
Example
In the query given below, we are retrieving the details of all the customers whose age is equal to the age of any customer whose name starts with ”K” −
SELECT * FROM CUSTOMERS WHERE AGE = ANY (SELECT AGE FROM CUSTOMERS WHERE NAME LIKE ''K%'');
Output
The result produced is as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
The SQL ALL Operator
The SQL ALL operator returns all the records of the SELECT statement.
-
It returns TRUE if the given condition is satisfied for ALL the values in the range.
-
It always returns a Boolean value.
-
It is used with SELECT, WHERE and HAVING statements in SQL queries.
-
The data type of the values returned from a subquery must be the same as the outer query expression data type.
Syntax
The basic syntax of the SQL ALL operator is as follows −
Column_name operator ALL (subquery);
Where,
-
column_name − is the name of a column in the main query.
-
operator − is a comparison operator such as =, <, >, <=, >=, or <>.
-
subquery − is a SELECT statement that returns a single column of values.
ALL with WHERE Statement
When we use the ALL operator with a WHERE clause, it filters the results of the subquery based on the specified condition.
The WHERE clause in SQL is used to filter rows from a query based on specific conditions. It operates on individual rows in the table, and it allows you to specify conditions that must be met by each row in the data returned by the query.
Example
If we consider the CUSTOMERS table created above,the following query returns the details of all the customers whose salary is not equal to the salary of any customer whose age is 25 −
SELECT * FROM CUSTOMERS WHERE SALARY <> ALL (SELECT SALARY FROM CUSTOMERS WHERE AGE = 25);
Output
The output of the above query is as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
ALL with HAVING Clause
In SQL, the ALL operator can also be used with the HAVING clause to filter the results of a GROUP BY query based on a condition that applies to all the aggregated values in the group.
Example
The following SQL query is used to obtain the details of all the customers whose salary is less than the average salary −
SELECT NAME, AGE, ADDRESS, SALARY FROM CUSTOMERS GROUP BY AGE, SALARY HAVING SALARY < ALL (SELECT AVG(SALARY) FROM CUSTOMERS);
Output
Output of the above query is as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
”;