;

SQL IN


In this tutorial, we will learn how to use IN operator with WHERE clause.

SQL IN Operator

SQL IN operator is used to filtering the result set based on the list of discrete/distinct values.

SQL IN operator is always used with the WHERE clause.

SQL IN operator is a shorthand for multiple OR conditions.

Syntax - Using values with IN operator
SELECT column1, column2,...columnN
FROM tableName
WHERE column IN (value1, value2,...valueN);
Syntax - Using SELECT statement with IN operator
SELECT column1, column2,...columnN
FROM tableName
WHERE column IN (SELECT STATEMENT);

Example:

Let us consider this table "Employee" for records.

Table Name: Employee

ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000
4 Kapil Noida India male 25000
5 Shalini Jaipur India female 18000
6 Rakesh Faridabad India male 23000
7 Akshay Mumbai India male 21000
8 Sarah New York US female 76000
9 Rocky Noida India male 28000

Example of IN Operator

Select all the employee whose city is "Delhi", "Noida" or "New York", a query will be:

Example - using values with IN operator
SELECT * FROM Employee
WHERE City IN ('Delhi', 'Noida', 'Newyork');
Output
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
4 Kapil Noida India male 25000
8 Sarah New York US female 76000
9 Rocky Noida India male 28000

Select all the employee whose city is not "Delhi", Noida" or "New York", a query will be:

Example
SELECT * FROM Employee
WHERE City NOT IN ('Delhi', 'Noida', 'Newyork');
Output
ID EmpName City Country Gender Salary
3 Ranvijay Mumbai India male 15000
5 Shalini Jaipur India female 18000
6 Rakesh Faridabad India male 23000
7 Akshay Mumbai India male 21000

Example IN Operator using subquery or SELECT statement 

Select all the employee whose city is "Delhi", "Noida" using select statement, a query will be:

Example - Using Subquery or SELECT statement with IN operator
SELECT * FROM Employee
WHERE City IN (SELECT City FROM Employee WHERE City='Noida' or City='Delhi');
Syntax - SELECT Statement
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
4 Kapil Noida India male 25000
9 Rocky Noida India male 28000