SQL Any Operator

In sql, Any operator is used to return a values which matches with any value in single column set of values. It’s like OR operator and it will compare a value against with any value in defined column.

 

To check any operator in sql with examples execute following script to create a new table in 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 above script, the new table’s example1 and example2 will be created and result will be like 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 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 above sql query, we will get a sql any operator result like as shown below.

 

SQL Any Operator Example Result or Output

 

In 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 above query, we will get the result like as shown below. 

 

SQL Server ANy Operator Example Result or Output

Topics Covered