SQL Any Operator

In SQL, Any operator is used to return values that match with any value in a single column set of values. It’s like an OR operator and it will compare a value against any value in the defined column.

 

To check any operator in SQL with examples execute the following script to create a new table in the SQL database.

 

create table example1(id int, name varchar(50))

insert into example1 values(1,'suresh'),(2,'dasari'),(3,'rohini'),(4,'madhav'),(5,'honey')

create table example2(id int, name varchar(50))

insert into example2 values(1,'suresh'),(4,'madhav'),(5,'honey')

 

select * from example1

select * from example2

Once we execute the above script, the new table’s example1 and example2 will be created and the result will be as shown below.

 

Create new table in sql server database

 

Now our tables are ready, to check how ANY operator will work in SQL with examples, then execute the following queries.

SQL Any Operator Example

Following is the example of using any operator in SQL.

 

SELECT id,name FROM example1

WHERE id = ANY(SELECT Id FROM example2)

When we execute the above SQL query, we will get a SQL any operator result like as shown below.

 

SQL Any Operator Example Result or Output

 

In the above query, the “id” column in example1 table will compare with all id's in example2 table like as shown below.

 

   (example1:id1 = example2:id1)

   OR (example1:id1 = example2:id4)

   OR (example1:id1 = example2:id5)

SQL Any Operator Example2

Following is the another example of using any operator in sql.

 

SELECT id,name FROM example1

id >= ANY(SELECT Id FROM example2)

When we execute the above query, we will get the result as shown below. 

 

SQL Server ANy Operator Example Result or Output