Back to: Oracle Tutorials for Beginners and Professionals
ANY Operator in Oracle with Examples
In this article, I am going to discuss ANY Operator in Oracle with Examples. Please read our previous article where we discussed DENSE_RANK Function in Oracle in Oracle. When a scalar value has to be compared with a single-column set of values then generally we use IN or JOINs. In addition to IN and JOIN, we can extend the comparison by using the ANY and ALL operators in Oracle. At the end of this article, you will understand what is ANY Operator and when and how to use ANY Operator in Oracle with Examples.
What is ANY Operator in Oracle?
The ANY Operator in Oracle is used to compare a value to each value in a list of results from a query and evaluate to true if the result of an inner query contains at least one row. The ANY operator must match at least one row in the subquery and must be preceded by comparison operators. For example, greater than (>) with ANY means greater than at least one value, similarly, less than (<) with ANY means less than at least one value. If this is not clear at the moment then don’t worry, we will try to understand the use of ANY Operator with examples.
The ANY operator in Oracle is used to compare any of the values in the given list. For better understanding, please have a look at the following image.
Syntax to use ANY Operator in Oracle:
Parameters:
- column_name: Name of the column of the table.
- expression1: Expression made up of a single constant, variable, scalar function, or column name and can also be the pieces of a SQL query that compare values against other values or perform arithmetic calculations.
- table_name: Name of the table.
- WHERE expression2: Compares a scalar expression until a match is found for ANY operator. One or more rows must match the expression to return a Boolean TRUE value for the ANY operator.
- comparison_operator: Compares the expression to the subquery. The comparison must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).
Understanding ANY Operator in Oracle with Examples:
Let us understand the ANY Operator in Oracle with examples. We are going to use the following PermanentEmployee and ContractEmployee tables to understand ANY Operator.
Please use the following SQL script to create PermanentEmployee and ContractEmployee table and populate these two tables with the required data.
CREATE TABLE PermanentEmployee ( EmployeeId INT, Name VARCHAR(20), Gender VARCHAR(10), Department varchar(10), Age INT ); INSERT INTO PermanentEmployee VALUES (1,'Pranaya','Male','IT',20); INSERT INTO PermanentEmployee VALUES (2,'Priyanka','Female','IT',22); INSERT INTO PermanentEmployee VALUES (3,'Sudhanshu','Male','HR',25); INSERT INTO PermanentEmployee VALUES (4,'Subrat','Male','Sales',60); INSERT INTO PermanentEmployee VALUES (5,'Tarun','Male','Sales',54); INSERT INTO PermanentEmployee VALUES (6,'Lipika','Female','HR',27); INSERT INTO PermanentEmployee VALUES (7,'Smita','Female','IT',40); INSERT INTO PermanentEmployee VALUES (8,'Smith','Male','HR',29); CREATE TABLE ContractEmployee ( EmployeeId INT, Name VARCHAR(20), Gender VARCHAR(10), Department varchar(10), Age INT ); INSERT INTO ContractEmployee VALUES (9,'Anurag','Male','IT',33); INSERT INTO ContractEmployee VALUES (11,'Sambit','Male','HR',29); INSERT INTO ContractEmployee VALUES (12,'James','Male','Sales',37); INSERT INTO ContractEmployee VALUES (13,'Pam','Female','Sales',25);
ANY Operator Example:
Here we have two tables i.e. PermanentEmployee and ContractEmployee and both the tables have the Age column. Now, if you want to fetch all the records from the PermanentEmployee table where the Age is at least greater than one value from the Age column of the ContractEmployee table. What would be your query? You can use the subquery and MIN function to write your query for the above requirement as shown below.
SELECT * FROM PermanentEmployee WHERE Age > (SELECT MIN(Age) FROM ContractEmployee);
Once you execute the above query, you will get the following output.
You can also use the Oracle ANY operator instead of using the Min function with the subquery to get the desired result. As you want to get all the rows from the PermanentEmployee table where Age is greater than any value of Age column in ContractEmployee table, You can use > Any Operator as shown below. Greater than ANY means greater than at least one value that is greater than the minimum.
SELECT * FROM PermanentEmployee WHERE Age > ANY (SELECT Age FROM ContractEmployee);
When you execute the above query, you will get the following output.
Here, we got the same records that were returned by our first query. If you will use =ANY that is equal to IN. With ANY you can use different comparison operators such as =, <>, !=, >, >=, !>, <, <=, !<.
In the next article, I am going to discuss ALL Operator in Oracle with examples. Here, in this article, I try to explain the ANY Operator in Oracle with Examples. I hope you enjoy this article.