IN, ALL, ANY operators in SQL
Hello, and welcome to the blog. In this blog we are going to talk about some operators in SQL which help us when we are are adding WHERE clause with large data.
This is going to be a good practice when we are going to learn about
"SUBQUERIES". This is going to be a very small but very useful blog.
So, In this blog we are going to talk about
- IN Operator
- BETWEEN Operator
- ALL Operator
- ANY Operator
So, let's get started,
1) IN Operator
- The IN operator allows you to specify multiple values in a WHERE clause.
- The IN operator is shorthand for multiple "OR" conditions.
-
let's jump to the syntax:SELECT column_nameFROM tableWHERE coulmn_name IN (value_1,value_2,...,value_n)
-
You can always replace values in syntax with a Subquery.
-
Let's see a example:Suppose we have to find Employees of IT and Sales department
-
Solution with IN operator:SELECT FirstNameFROM Employee1
-
Solution without IN operator:SELECT FirstNameFROM Employee1
-
We got the same result it is just that we saved a couple of seconds of writing query with IN operator. This couple of seconds turns to couple of minutes when we will advance our skills.
2) BETWEEN Operator
- As the name suggests BETWEEN operator selects records with value within a given range.
- The values can be numbers, text or dates.
-
let's see the syntax:SELECT column_nameFROM tableWHERE coulmn_name BETWEEN value_1 AND value_2
-
Let's see a example:Suppose we have to find Employees of Ranging from 30,000 to 50,000
-
Solution :SELECT FirstName, SalaryFROM Employee1
-
Now I want to make one more point here : BETWEEN operator is inclusive that means it will also check for values which are there in query.
-
You can see in the result we also have employees with salary as 50000 in our result set.
3) ALL Operator
- ALL operator provides you "AND" functionality with conditional operators ('<', '>', '=', '<=', '>=', '!=')
- When you use ALL operator all values must satisfy the condition.
- This operator can only be used with "Subquery". We will learn Subquery in upcoming blogs.
-
let's see the syntax:SELECT column_nameFROM tableWHERE coulmn_name conditional_operator ALL (subquery)
-
Example:
SELECT FirstName FROM Employee1 WHERE DepartmentId = ALL ( SELECT Id FROM Department WHERE DepartmentName IN ( 'IT' ,'Sales' ) )
-
As Id field can never hold multiple values we got nothing in result set.
4)ANY Operator
- ANY is exactly same as ALL operator in terms of rules and syntax, but its different in functionality.
- The ANY operator returns TRUE if any of the subquery value meet the condition
- Let's understand it better by using the same query which we have used in ALL. We just have to replace ALL with ANY.
-
Example:
SELECT FirstName FROM Employee1 WHERE DepartmentId = ANY ( SELECT Id FROM Department WHERE DepartmentName IN ( 'IT' ,'Sales' ) )
Congratulations learning some more topics in SQL. See you in
some another blog till then "Keep coding, keep learning because trust me you can make it big".
"Have a great day"




Good
ReplyDeleteVery informative sir... thank you 👍
ReplyDelete