In this article I will explain about IN operator in Sql Server with example

It helps to specify multiple values in where clause it determines whether a specified value matches any value in a subquery or a list The SQL IN condition allows you to easily test if an expression matches any value in a list of values. It helps to reduce the need for multiple OR conditions in a DML statement. 

Syntax for IN Condition 

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
 
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

 

 

Let us take an example to see the implementation 

First create table E_Salary 

CREATE TABLE E_Salary(
     [Id] [int] IDENTITY(1,1) NOT NULL,
     [S_Id] [varchar](50) NULL,
     [Name] [varchar](50) NULL,
     [City] [varchar](50) NULL,
     [Salary] [varchar](50) NULL,
     )

 

I have inserted some records in it shown in below screen shot

 

 

Suppose I have to display name and salary who belongs to Allahabad

We can get this result by using IN operator

Like 

select name,Salary from E_Salary where City IN('allahabad')

output

 

 

 

We can use IN operator with NOT operator 

Suppose we have to get those record who not belongs to Allahabad 

select name,Salary from E_Salary where City Not IN('allahabad')


 

  Modified On Mar-27-2018 04:03:08 AM

Leave Comment