SQL - ANY, ALL Operators


Operators in SQL have the same meaning as that of operators in mathematics. They are keywords that are used in SQL statements for performing comparisons or logical operations. In SQL, there are four types of operators in SQL. Arithmetic operator, Comparison operator, Logical Operator, and Bitwise Operators.

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. These are the logical operators.

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 (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Komal', 22, 'MP', 4500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (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 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

Now, let us try to 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 | MP      |  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 trying to find 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 |
+-----+
|  22 |
|  23 |
|  25 |
|  32 |
+-----+

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 trying to return 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 | MP      | 4500.00 |
+----+----------+-----+---------+---------+

The SQL - ALL operator

The 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.

Now, let us look at few examples of SQL- ALL operator.

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 | MP        |  4500.00 |
|  7 | Muffy   |  24 | Indore    | 10000.00 |
+----+---------+-----+-----------+----------+

ALL with HAVING statement

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 NAME, AGE, ADDRESS, SALARY
HAVING SALARY < ALL (SELECT avg(SALARY) from CUSTOMERS);

Output

Output of the above query is as follows −

+---------+-----+-----------+---------+
| NAME    | AGE | ADDRESS   | SALARY  |
+---------+-----+-----------+---------+
| kaushik |  23 | Kota      | 2000.00 |
| Khilan  |  25 | Delhi     | 1500.00 |
| Komal   |  22 | MP        | 4500.00 |
| Ramesh  |  32 | Ahmedabad | 2000.00 |
+---------+-----+-----------+---------+

Note − In SQL server, whenever we use an aggregate function, all columns of the table used in the SELECT statement must be present in the GROUP BY clause to summarize the table based on the aggregate functions used.

Advertisements