How to use SQL IN Operator

How to use SQL IN Operator

3 mins read349 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Nov 22, 2022 19:49 IST

In the previous article, we have discussed LIKE operator. In this article, we will discuss IN operator in SQL that is also a logical operator.

2022_07_SQL-OPERATORS-1.jpg

Table of Content

Recommended online courses

Best-suited Data Analytics courses for you

Learn Data Analytics with these high-rated online courses

2.25 L
3 years
2.25 L
3 years
1.18 L
12 months
35 K
3 months
2.6 L
12 months
97 K
4 months

What is an IN operator?

IN operator is a logical operator in SQL that is used to select the rows whose values are in a set of values i.e. IN operator allows you to specify a list of values that you want to include in your result.

  • It is used with the WHERE clause
  • It reduces the need for multiple OR clauses (condition)
  • It can be used with subqueries

Must Read: SQL LIKE

Must Read: SQL BETWEEN

Syntax

 
SELECT column_names
FROM table_names
WHERE column_name IN (value_1, value_2, value_3,.....,value_n);
Copy code

Syntax of IN operator for subquery

 
SELECT column_names
FROM table_name
WHERE condition IN (SELECT query);
Copy code

Now, we will take some examples to get a better understanding of how to use IN operator in SQL

Here, we will take the Employee (that contains Employee ID, Name, Gender, Department, Education, Month of Joining, and CTC) and Department Dataset (DepartmentID and Department Name) to do some examples to get a clear understanding of different logical operators.

EmployeeID Name Gender DepartmentID Education Month of Joining CTC(in Lacs)
1001 Ajay M 1 Doctoral January 25
1002 Babloo M 1 UG February 23
1003 Chhavi F 2 P March 15
1004 Dheeraj M 2 UG January 12
1005 Evina F 3 UG March 16
1006 Fredy M 4 UG December 10
1007 Garima F 4 PG March 10
1008 Hans M 5 PG November 8
1009 Ivanka F 5 Intermediate June 7
1010 Jai M 6 December 4
Employee
DepartmentID Department Name
1 Engineering
2 HR
3 Marketing
4 Sales
5 Admin
6 Peon
Department

Must Read: Introduction to SQL

Must Read: SQL ACID Properties

IN operator with Numeric Values

Example 1: Find the employee id, name, and department where CTC is 15, 12, and 10 lacs.

Query

 
SELECT EmployeeID, Name, DepartmentID
FROM Employee
WHERE CTC IN (15, 12, 10);
Copy code

Output

EmployeeID Name DepartmentID
1003 Chhavi 2
1004 Dheeraj 2
1006 Fredy 4
1007 Garima 4

Must Read: What is the difference between SQL and MySQL?

Must Read: Difference between SQL and NoSQL

IN operator with string

Example 2: Find the EmployeeID, Name, and Month of Joining who join in the month of January, February, and November.

Query

 
SELECT EmployeeID, Name, Month of Joining
FROM Employee
WHERE Month of Joining IN (‘January’, ‘February’, ‘November’);
Copy code

Output

EmployeeID Name Month of Joining
1001 Ajay January
1002 Babloo February
1004 Dheeraj January
1008 Hans November

Must Read: Subqueries in SQL

Must Read: SQL CREATE TABLE

OR vs IN

Example 3: In this example, we will take the same example 2 but write the query using OR.

Find the EmployeeID, Name, and Month of Joining who join in the month of January, February, and November using OR.

Query

 
SELECT EmployeeID, Name, Month of Joining
FROM Employee
WHERE Month of Joining = ‘January’ OR
Month of Joining = ‘February’ OR
Month of Joining = ‘November’;
Copy code

Output

EmployeeID Name Month of Joining
1001 Ajay January
1002 Babloo February
1004 Dheeraj January
1008 Hans November

Must Read: SQL WHERE

Must Read: Introduction to Normalization

NOT with IN

Example 4: Find the name, education, and CTC of all the employees except those who are not belonging to departmentID 2, and 4 .

Query

 
SELECT Name, Education, CTC
FROM Employee
WHERE DepartmentID NOT IN (2, 4);
Copy code

Output

Name Education CTC
Ajay Doctoral 25
Babloo UG 23
Evina UG 16
Hans PG 8
Ivanka Intermediate 7
Jai 4

Must Read: SQL LIMITS

Must Check: SQL Online Course and Certifications

IN operator with Subquery

Query

 
SELECT EmployeeID, Name, Gender
FROM Employee
WHERE DepartmentID IN (SELECT Department Name FROM Department WHERE DepartmentID > 3);
Copy code

Output

EmployeeID Name Gender Department Name
1006 Fredy M Sales
1007 Garima F Sales
1008 Hans M Admin
1009 Ivanka F Admin
1010 Jai M Peon

Must Read: SQL Comparison and Arithmetic Operator

Conclusion

In this article, we have discussed how to use IN operator in sql with the help of examples.

Hope this article, will help you in Data Science/Data Analysis journey.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio