×

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s

The IN operator in SQL also allows to easily test the condition matches any value in a list of values. It reduces the number of OR operators in SQL queries.

Syntax of IN operator in SQL:

SELECT COLUMNNAME FROM TABLENAME WHERE COLUMNNAME IN (VALUE1, VALUE2);

Subquery Syntax of IN operator in SQL:

SELECT COLUMNNAME FROM TABLENAME WHERE COLUMNNAME IN (SELECT STATEMENT); 

In the above syntax, we can use IN operator with subquery also.

Let’s understand the SQL SELECT IN concept with the help of examples.

Consider the following tables which have the following records:

Table 1: Employees

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Table 2: Manager

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING
5Akash KadamC#

Example 1:  Write a query to fetch the employee id, first name, last name, and City from the employee's table where City includes one of the lists is Mumbai, Pune, and Delhi in the table.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, CITY FROM EMPLOYEES WHERE CITY IN ('MUMBAI', 'PUNE', 'DELHI');

In the above statement, we have retrieved the employee id, first and last name, and City from the employee's table, where City includes one of the city names, Mumbai, Pune, and Delhi. All these cities names are passed as a parameter in the IN operator as values. The table will go for all those records whose employees' cities match the IN operator parameter city list.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMECITY
1001VAIBHAVIMISHRAPUNE
2002BHAVESHJAINPUNE
2003RUCHIKAJAINMUMBAI
3001PRANOTISHENDEPUNE
3003DEEPAMJAUHARIMUMBAI
4001RAJESHGOUDMUMBAI
4003RUCHIKAAGARWALDELHI
5001ARCHITSHARMADELHI
SQL SELECT IN

It shows only eight employees whose city names match the IN operator expression parameter list.

Example 2: Write a query to fetch the employee id, first name, last name, salary, and Department from the employee's table where salary includes one of the lists is 50500, 55500, and 65500 in the table.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, DEPARTMENT FROM EMPLOYEES WHERE SALARY IN (50500, 55500, 65500);

In the above statement, we have retrieved the employee id, first and last name, salary, and Department from the employee's table where salary includes one of the salaries are 50500, 55500, and 65500. All these salaries are passed as a parameter in the IN operator as values. The table will go for all those records whose employees' salaries match the IN operator parameter salary list.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYDEPARTMENT
1001VAIBHAVIMISHRA65500ORACLE
1003NIKHILVANI50500FMW
2001PRACHISHARMA55500ORACLE
2002BHAVESHJAIN65500FMW
3001PRANOTISHENDE55500JAVA
3002ANUJAWANRE50500FMW
5001ARCHITSHARMA55500TESTING
SQL SELECT IN

It shows only seven employees whose salary matches the IN operator expression parameter list.

Example 3: Write a query to fetch the employee id, first name, last name, salary, and City from employees where employee salary is greater than 60000 or employee city includes one of the city lists is 'Mumbai', 'Pune' and, 'Jaipur' in the table.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, CITY FROM EMPLOYEES WHERE SALARY > 60000 OR CITY IN ('PUNE', 'MUMBAI', 'JAIPUR');

In the above statement, we have fetched employee id, first name, last name, salary, and City from employees table where employee salary is greater than 60000 or employee city includes one of this city list is 'Mumbai', 'Pune', 'Jaipur'. The SELECT statement WHERE clause first part is SALARY > 60000 and last part is CITY IN ('PUNE', 'MUMBAI', 'JAIPUR'), aswe used OR operator the result will be from both the conditions.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITY
1001VAIBHAVIMISHRA65500PUNE
1003NIKHILVANI50500JAIPUR
2002BHAVESHJAIN65500PUNE
2003RUCHIKAJAIN50000MUMBAI
3001PRANOTISHENDE55500PUNE
3002ANUJAWANRE50500JAIPUR
3003DEEPAMJAUHARI58500MUMBAI
4001RAJESHGOUD60500MUMBAI
SQL SELECT IN

It shows only eight records from the table whose salary is greater than 60000 or City names match the IN operator expression parameter list.

Example 4: Write a query to retrieve employee id, Salary, City, and Department from employees table where employee department includes one of the lists is 'Oracle', 'FMW' and also City includes one of the lists is 'Delhi', 'Noida', 'Pune'.

SELECT EMPLOYEEID, SALARY, CITY, DEPARTMENT FROM EMPLOYEES WHERE DEPARTMENT IN ('ORACLE', 'FMW') AND CITY IN ('PUNE', 'NOIDA', 'DELHI');

We have retrieved the employee id, salary, City, and Department from the employee's table in the above statement. The employee department includes one of the department lists passed to the IN operator parameter. Employee city includes one of the city lists passed to the IN operator parameter, and the result includes only those employee details that match both the conditions.

Output:

EMPLOYEEIDSALARYCITYDEPARTMENT
100165500PUNEORACLE
200265500PUNEFMW
400360000DELHIORACLE
SQL SELECT IN

There are only three records from the Employees table whose employee city includes Pune, Delhi, and Noida, and Department includes Oracle, FMW. 

Example 5: Write a query to fetch employee id, first name, last name, salary, City, and Department from the employee's table where employee salary is greater than 60000 and City includes one of the lists in Pune, Jaipur, Mumbai or Department includes one of the lists is Java, Testing, C#.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, CITY, DEPARTMENT FROM EMPLOYEES WHERE SALARY > 60000 AND CITY IN ('PUNE', 'MUMBAI', 'JAIPUR') OR DEPARTMENT IN ('JAVA', 'TESTING', 'C#');

In the above statement, we have used OR operator, AND operator, and multiple IN operator with the SELECT statement to fetch the employee id, first name, last name, salary, City, and Department from the employee's table. The SELECT query first fetches the employee records where salary > 60000 AND City IN ('Pune', 'Mumbai', Jaipur), only those employees. Both conditions are true, and at the end Department IN ('Java', 'Testing', 'C#'), this query will search in the first phase result that those employee records we retrieved having Department one of the lists we passed to the IN operator and also go for the rest of the records in the table if any records found having department name we passed to the IN operator parameter that record will be added to the result.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENT
1001VAIBHAVIMISHRA65500PUNEORACLE
1002VAIBHAVSHARMA60000NOIDAC#
2002BHAVESHJAIN65500PUNEFMW
2003RUCHIKAJAIN50000MUMBAIC#
3001PRANOTISHENDE55500PUNEJAVA
3003DEEPAMJAUHARI58500MUMBAIJAVA
4001RAJESHGOUD60500MUMBAITESTING
4002ASHWINIBAGHAT54500NOIDAJAVA
5001ARCHITSHARMA55500DELHITESTING
SQL SELECT IN

There are only 9 records of employees whose salary is greater than 60000. City includes one of the lists we passed as a parameter to the City IN operator or Department includes one of the lists we passed to the Department IN operator.

Example 6: Write a sub-query to fetch employee details from the employee table where managerid is greater than 2 from the manager table.

SELECT * FROM EMPLOYEES WHERE MANAGERID IN (SELECT MANAGERID FROM MANAGER WHERE MANAGERID > 2); 

In the above statement, the First subquery will get executed SELECT MANAGERID FROM MANAGER WHERE MANAGERID > 2; the output will be manager id which is greater than 2 pass as a parameter in the main query WHERE clause, and the final output will be from employees table where employee-manager id includes one of the lists which is the output of sub-query.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
3001PRANOTISHENDE55500PUNEJAVA3
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
5001ARCHITSHARMA55500DELHITESTING4
1002VAIBHAVSHARMA60000NOIDAC#5
2003RUCHIKAJAIN50000MUMBAIC#5
SQL SELECT IN

There are only seven records from the employee's table whose manager id is greater than 2.

Example 7: Write a sub-query to fetch the employee’s details where the manager department includes one of the lists is an oracle, java, and FMW.

SELECT * FROM EMPLOYEES WHERE MANAGERID IN (SELECT MANAGERID FROM MANAGER WHERE MANAGER_DEPARTMENT IN ('ORACLE', 'FMW', 'JAVA'));

In the above statement, we first fetch the manager id from the manager table where the manager department includes one of the lists is Oracle, FMW, Java. Then the main query will fetch the employee's details from the output of the sub-query.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
2001PRACHISHARMA55500CHANDIGARHORACLE1
4003RUCHIKAAGARWAL60000DELHIORACLE1
1003NIKHILVANI50500JAIPURFMW2
2002BHAVESHJAIN65500PUNEFMW2
3002ANUJAWANRE50500JAIPURFMW2
3001PRANOTISHENDE55500PUNEJAVA3
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
SQL SELECT IN

Example 8: Write a query to fetch employee id, first name, last name, salary, City from the employee's table where salary is between 50000 and 65000 or City includes one of the lists is Pune, Jaipur, and Mumbai.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, CITY FROM EMPLOYEES WHERE SALARY BETWEEN 50000 AND 65000 OR CITY IN ('PUNE', 'MUMBAI', 'JAIPUR');

In the above statement, we have fetched the employee id, first name, last name, salary, and City from the employee's table where employee salary between 50000 and 65000 or the City includes one of the lists is Pune, Mumbai, Jaipur.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITY
1001VAIBHAVIMISHRA65500PUNE
1002VAIBHAVSHARMA60000NOIDA
1003NIKHILVANI50500JAIPUR
2001PRACHISHARMA55500CHANDIGARH
2002BHAVESHJAIN65500PUNE
2003RUCHIKAJAIN50000MUMBAI
3001PRANOTISHENDE55500PUNE
3002ANUJAWANRE50500JAIPUR
3003DEEPAMJAUHARI58500MUMBAI
4001RAJESHGOUD60500MUMBAI
4002ASHWINIBAGHAT54500NOIDA
4003RUCHIKAAGARWAL60000DELHI
5001ARCHITSHARMA55500DELHI
SQL SELECT IN

Related Topics

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.