What is the difference between IN
and ANY
operators in SQL?
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…..);
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.
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.
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.
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 >=
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:
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With