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
  1. IN Operator
  2. BETWEEN Operator
  3. ALL Operator
  4. ANY Operator
Currently our Employee1 table look like this:


So, let's get started,

1) IN Operator

  1. The IN operator allows you to specify multiple values in a WHERE clause.
  2. The IN operator is shorthand for multiple "OR" conditions.
  3. let's jump to the syntax:
    SELECT column_name
    FROM table
    WHERE coulmn_name IN (value_1,value_2,...,value_n)
  4. You can always replace values in syntax with a Subquery.
  5. Let's see a example:
    Suppose we have to find Employees of IT and Sales department
  6. Solution with IN operator:
    SELECT FirstName
    FROM Employee1
    WHERE DepartmentId IN (2, 4)

  7. Solution without IN operator:
    SELECT FirstName
    FROM Employee1
    WHERE DepartmentId =1 OR DepartmentId=2

  8. 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

  1. As the name suggests BETWEEN operator selects records with value within a given range.
  2. The values can be numbers, text or dates.
  3. let's see the syntax:
    SELECT column_name
    FROM table
    WHERE coulmn_name BETWEEN value_1 AND value_2
  4. Let's see a example:
    Suppose we have to find Employees of Ranging from 30,000 to 50,000
  5. Solution :
    SELECT FirstName, Salary
    FROM Employee1
    WHERE Salary BETWEEN 30000 AND 50000

  6. 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.
  7. You can see in the result we also have employees with salary as 50000 in our result set.

3) ALL Operator

  1. ALL operator provides you "AND" functionality with conditional operators ('<', '>', '=', '<=', '>=', '!=')
  2. When you use ALL operator all values must satisfy the condition.
  3. This operator can only be used with "Subquery". We will learn Subquery in upcoming blogs.
  4. let's see the syntax:
    SELECT column_name
    FROM table
    WHERE coulmn_name conditional_operator ALL (subquery)
  5. Example:
    SELECT FirstName
    FROM Employee1
    WHERE DepartmentId = ALL (
    		SELECT Id
    		FROM Department
    		WHERE DepartmentName IN (
    				'IT'
    				,'Sales'
    				)
    		)
    

  6. As Id field can never hold multiple values we got nothing in result set.

4)ANY Operator

  1. ANY is exactly same as ALL operator in terms of rules and syntax, but its different in functionality.
  2. The ANY operator returns TRUE if any of the subquery value meet the condition
  3. Let's understand it better by using the same query which we have used in ALL. We just have to replace ALL with ANY.
  4. 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"

Comments

Post a Comment

Popular posts from this blog

SQL Joins | Inner Join | Left Join | Right Join | Full Join | Self Join

Update, Delete and Truncate Commands

More clauses in SQL