Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between IN and ANY operators in SQL

Tags:

operators

sql

any

What is the difference between IN and ANY operators in SQL?

like image 740
Jagan Avatar asked Sep 13 '10 09:09

Jagan


People also ask

What is difference between in and operator?

IN Operator Instead of using, multiple ORs, we have IN keyword to select only those records whose column's value matches with given values. Like BETWEEN keyword, It also reduces the length of statements. Syntax: SELECT * FROM tableName WHERE name IN(value,value1,value2…..);

What is the IN operator in SQL?

The SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

What is the difference between any and exists in SQL?

Exists is same as any except for the time consumed will be less as, in ANY the query goes on executing where ever the condition is met and gives results . In case of exists it first has to check throughout the table for all the records that match and then execute it.

What is the difference between in and not in operator?

The EXCEPT operator removes duplicate rows from the results and returns only DISTINCT records. On the other hand, the NOT IN operator will return duplicate records. Let's take a look at this with the help of an example.


2 Answers

SQL> SQL> -- Use the ANY operator in a WHERE clause to compare a value with any of the values in a list. SQL> 

SQL> -- You must place an =, <>, <, >, <=, or >= operator before ANY.

SQL> SELECT *   2  FROM employee   3  WHERE salary > ANY (2000, 3000, 4000); 

For In Operator

SQL> -- Use the IN operator in a WHERE clause to compare a value with any of the values in a list. SQL> SELECT *   2  FROM employee   3  WHERE salary IN (2000, 3000, 4000); 

But with the IN operator you cannot use =, <>, <, >, <=, or >=

like image 79
Pranay Rana Avatar answered Sep 21 '22 09:09

Pranay Rana


IN - Equal to anything in the list

ANY - Compares value to each value returned by the sub query.

ALL - Compares value to every value returned by the sub query.

For example:

IN:

Display the details of all employees whose salaries are matching with the least investments of departments?

 Select Ename, Sal, Deptno   from Emp   Where Sal IN (Select Min(Sal)                 From Emp                 Group By Deptno); 

ANY:

< ANY means less than the maximum value in the list.

Get the details of all employees who are earning less than the highest earning manager?

 Select Empno, Ename, Job, Sal   From Emp  Where Sal < Any (Select Distinct MGR                    From Emp); 

> ANY means more than the minimum value in the list.

Get the details of all employees who are earning more than the least paid in Department 10?

 Select Empno, Ename, Job, Sal   From Emp  Where Sal > Any (Select Min(Sal)                    From Emp                    Where Deptno 10); 

= ANY is equivalent to in operator.

Note: SOME can also be used instead of ANY.

like image 38
Tejas Patel Avatar answered Sep 24 '22 09:09

Tejas Patel