Tuesday, August 18, 2015

Between and IN Operators in SQL Server

BETWEEN and IN comparison:

BETWEEN will be used whenever we want to select the data based on a range.

IN will be used, if we want to select the data that falls under some random values.

Example:
Fetch the employees whose ID is from 1 to 3:
SELECT
       *
FROM
       dbo.Employee
WHERE
       EmployeeID BETWEEN 1 AND 3



Fetch the employees whose ID is either 1 OR 3 OR 5
SELECT
       *
FROM
       dbo.Employee
WHERE
       EmployeeID IN (1, 3, 5)


If we don't have IN, BETWEEN clause we need to write the same query by using OR keyword which will increase length of your code.
SELECT
       *
FROM
       dbo.Employee
WHERE
       EmployeeID = 1
       OR EmployeeID = 3
       OR EmployeeID = 5



Labels: , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home