Web Analytics Made Easy - Statcounter
With IN clause, we can specify multiple values in a WHERE clause.

For example, in case of “IN”, one can use the subquery to get the multiple values in the IN clause or we can have predefined values.

In this article, we are going to use the following table for some examples:-

CREATE TABLE [dbo]. [Employee](
[Empid] [Int] IDENTITY (1, 1) NOT NULL Primary key,
[EmpNumber] [nvarchar](50) NOT NULL,
[EmpFirstName] [nvarchar](150) NOT NULL,
[EmpLAStName] [nvarchar](150) NULL,
[EmpEmail] [nvarchar](150) NULL,
[Salary] [int] NULL
)

Suppose, we have another table Empprojects, which contains the employees and their project relationship and it has the below table structure

create table Empprojects (empprojectid int identity(1,1), empnumber nvarchar (50), projectcode nvarchar(50))

Now suppose we want to get the records for the employees which are associated with the project having code “Proj002” or “Proj005”, then we can use the following query:-

select * from employee where empnumber in (select empnumber from Empprojects where projectcode=’Proj002′ or projectcode=’Proj005′)

In the above query, we use the subquery “select empnumber from Empprojects where projectcode=’Proj002′ or projectcode=’Proj005′ ” to fetch the multiple values in the IN clause.

Also suppose, we want to get the records of employee who worked in the first project having code ‘Proj001’. We can use subquery for it as we see in the above example. But we know that that project is completed years back and only 3 employees having empnumber “A001” , “A002” and “A003” worked on it , then we can pass the empnumber of these 3 employee as predefined values in the IN clause as shown in the below query:-

select * from employee where empnumber In (‘A001′,’A002′,’A003’)


Discover more from Technology with Vivek Johari

Subscribe to get the latest posts sent to your email.

By vivekjohari

I am currently working as a Senior Database Professional and have around 18 years of experience in database. Degree:- Master Degree in Computer(MCA) Certification course in Data Science & Machine Learning from Indian Institute of Technology (IIT), Delhi Work experience:- Designing of the database. Database Optimization. Writing Complex Stored Procedures,Functions,Triggers etc. Designing and developing SSIS & DTS packages. Designing SQL Reports using SSRS. Database Server Maintenance. Certification:- MCTS: DA-100: Analysing Data with Microsoft Power BI MCTS: DP-300: Administering Relational Databases on Microsoft Azure Microsoft certified Sql DBA in Sql server 2008 (MCTS). Microsoft certified BI professional in Sql server 2008 (MCTS). Oracle certified profession DBA in ORACLE 10g (OCP) certified profession DBA in ORACLE 9i (OCP) My other publication Technical Blog:- Technologies with Vivek Johari Guest Author and Blogger at sqlservercentral.com

32 thought on “IN clause in SQL Server”
  1. … [Trackback]

    […] There you can find 70043 additional Info on that Topic: techmixing.com/2014/08/in-clause-in-sql-server.html […]

  2. … [Trackback]

    […] Here you can find 62645 more Information on that Topic: techmixing.com/2014/08/in-clause-in-sql-server.html […]

Leave a Reply

Discover more from Technology with Vivek Johari

Subscribe now to keep reading and get access to the full archive.

Continue reading