The Wayback Machine - https://web.archive.org/web/20241005225614/https://www.geeksforgeeks.org/sql-in-operator/
Open In App

SQL IN Operator

Last Updated : 18 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The SQL IN operator filters data based on a list of specific values. In general, we can only use one condition in the WHEN clause, but the IN operator allows us to specify multiple values.

In this article, we will learn about the IN operator in SQL by understanding its syntax and examples.

IN Operator

The IN Operator in SQL is used to specify multiple values/sub-queries in the WHERE clause. It provides an easy way to handle multiple OR conditions.

We only pass a single condition in the WHERE clause, however there might be situations where we need to select data based on multiple conditions. For such cases, the IN operator is used.

Note: If any of the conditions are passed using the IN operator, they will be considered true

SQL IN Syntax

The Syntax of the IN operator is as follows:

SELECT column_name FROM table_name

WHERE condition IN (condition_value1, condition_value2 …..);

Here, we select the column column_name from the table table_name where the condition is checked in all the condition_values passed with the IN operator.

DEMO SQL Database

We will use the following SQL table for our examples below:

FnameLnameSsnBdateAddressSexSalary

Chiranjeev

Singh

1

2002-07-31

Delhi

M

1111789.00

Harry

Stark

2

1990-07-31

Delhi

M

3333.00

Meghna

Gururaani

5

2002-04-04

Almora

F

3433.00

Aniket

Bhardwaj

6

2001-05-05

Ponta

M

56564.00

Vritti

Goel

7

2002-03-05

Delhi

F

7565.00

Aashish

Kumar

8

2002-08-04

Himachal

M

44657.00

Siddharth

Chaturvedi

9

2003-11-10

Lucknow

M

244322.00

You can create the following table using these queries:

MySQL
CREATE TABLE Employee (
    Fname VARCHAR(50),
    Lname VARCHAR(50),
    Ssn INT,
    Bdate DATE,
    Address VARCHAR(100),
    Sex CHAR(1),
    Salary DECIMAL(10, 2)
);
INSERT INTO Employee (Fname, Lname, Ssn, Bdate, Address, Sex, Salary) VALUES 
('Chiranjeev', 'Singh', 1, '2002-07-31', 'Delhi', 'M', 1111789.00),
('Harry', 'Stark', 2, '1990-07-31', 'Delhi', 'M', 3333.00),
('Meghna', 'Gururaani', 5, '2002-04-04', 'Almora', 'F', 3433.00),
('Aniket', 'Bhardwaj', 6, '2001-05-05', 'Ponta', 'M', 56564.00),
('Vritti', 'Goel', 7, '2002-03-05', 'Delhi', 'F', 7565.00),
('Aashish', 'Kumar', 8, '2002-08-04', 'Himachal', 'M', 44657.00),
('Siddharth', 'Chaturvedi', 9, '2003-11-10', 'Lucknow', 'M', 244322.00);

SQL IN Operator Examples

Let’s look at some examples of IN operator in SQL and understand its working.

Example 1:

SQL Query to get Fname and Lname of employees who have address in Delhi and Himachal

Query:

SELECT Fname, Lname FROM employee
WHERE Address IN ('Delhi','Himachal');

Output:

FnameLname

Chiranjeev

Singh

Harry

Stark

Vritti

Goel

Aashish

Kumar

In this query, we fetched the Fname and Lname of all the employees whose address is either Delhi or Himachal. To do so, we use the IN operator to pass these values to the WHERE clause.

Example 2: SQL IN and NOT IN Operators

We can use the SQL IN with the NOT operator to exclude specified data from our result.

Query:

SELECT Fname FROM employee
WHERE Address NOT IN ('Delhi', 'Lucknow');

Output:

Fname

Meghna

Aniket

Aashish

Here, we have created a query to fetch the Fname of all employees whose address is neither Delhi nor Lucknow. We used the NOT operator with IN to exclude these values.

Example 3:

We have used IN operator with explicit values for conditions. However, we can use the IN operator to select values for the condition from another query. For demonstrating this, we will use another table from the database, manager. The contents of the table are given below.

SsnDepartment

5

Sales

1

Technical

Now, we will write a query to fetch details of all employees who are managers. This can be done by using nested SELCT queries with the IN operator.

Query:

SELECT * FROM employee
WHERE Ssn IN (SELECT Ssn FROM manager);

Output:

FnameLnameSsnBdateAddressSexSalary

Chiranjeev

Singh

1

2002-07-31

Delhi

M

1111789.00

Meghna

Gururaani

5

2002-04-04

Almora

F

3433.00

Here, we have selected the Ssn of all managers from the manager table and then, we have passed the result of this query to the main query in the IN operator, which will fetch the details of all employees who have same Ssn as fetched from the nested query.

Key takeaways about the SQL IN operator:

  • The SQL IN operator allows you to specify multiple values in a WHERE clause.
  • It checks if a specified value matches any value in a list.
  • It simplifies querying for records that match multiple criteria without needing to use multiple OR conditions.
  • The syntax is straightforward: WHERE column_name IN (value1, value2, ...).
  • It is commonly used with SELECT, INSERT, UPDATE, and DELETE statements for filtering or updating data based on multiple values.
  • Using the IN operator can make SQL queries more concise and readable.


    Previous Article
    Next Article

    Similar Reads

    Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
    Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
    2 min read
    Configure SQL Jobs in SQL Server using T-SQL
    In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
    7 min read
    SQL | Concatenation Operator
    Prerequisite: Basic Select statement, Insert into clause, SQL Create Clause, SQL Aliases || or concatenation operator is use to link columns or character strings. We can also use a literal. A literal is a character, number or date that is included in the SELECT statement. Let's demonstrate it through an example: Syntax: SELECT id, first_name, last_
    2 min read
    SQL | Alternative Quote Operator
    This post is a continuation of the SQL Concatenation Operator. Now, suppose we want to use apostrophe in our literal value but we can't use it directly. See Incorrect code: SELECT id, first_name, last_name, salary, first_name||' has salary's '||salary AS "new" FROM one So above we are getting error, because Oracle server thinking that the first apo
    3 min read
    Inequality Operator in SQL
    Pre-requisite - Operators in SQL In this article, we will discuss the overview of operators in SQL, and mainly our focus will be on Inequality Operators in SQL. Let's discuss them one by one.SQL Operators are widely used to access information on the basis of conditions. In the Inequality operator, we will check the condition on a specific column. O
    3 min read
    How to Use the IN Operator with a SubQuery in SQL?
    In this article, we will see the use of IN Operator with a SubQuery in SQL. IN operator is used to compare the column values with the list of values. The list of values is defined after IN query in SQL. If we don't know the exact list of values to be compared, we can generate the list of values using the query. Syntax: Without subquery: SELECT colu
    2 min read
    SQL IS NOT NULL Operator
    In SQL, the IS NOT NULL Operator is a logical operator. Its application is just the opposite of IS NULL Operator. Unlike IS NULL Operator, this operator returns True, if the value is not a NULL value. It is usually used with a WHERE clause to filter the table data. It filters out the data and separates it from records where specified columns have N
    5 min read
    SQL LIKE Operator
    SQL LIKE operator is used with the WHERE clause to search for a specified pattern in a column. LIKE operator finds and returns the rows that fit in the given pattern. LIKE operator is case-insensitive by default in most database systems. This means that if you search for "apple" using the LIKE operator, it will return results that include "Apple",
    4 min read
    SQL NOT Operator
    SQL NOT Operator is used to return the opposite result or negative result. It is a logical operator in SQL, that negates the boolean expression in the WHERE clause. It is mostly used to specify what should not be included in the results table. NOT SyntaxSELECT column1, colomn2, … FROM table_name WHERE NOT condition; Demo SQL DatabaseBelow is a sele
    2 min read
    Difference between (Equal) = and IN operator in SQL
    Equal (=) and IN operators are both comparison operators in SQL, the only difference between = and IN operator is that '=' operator compares two values, but IN operator compares a value against set of values. Here, we will learn the difference between the SQL '=' and IN operator by understanding each of them. Difference between = and IN Operator= O
    2 min read
    SQL NOT EQUAL Operator
    SQL NOT EQUAL Operator is a comparison operator used to check whether two expressions are equal. This operator is represented by " != " or "<>". NOT EQUAL Operator in SQLNOT EQUAL Operator in SQL is used to compare two values and return if they are not equal. This operator returns boolean values. If given expressions are equal, the operator r
    4 min read
    SQL UNION Operator
    SQL UNION operator combines result sets of two or more SELECT statements into one results set. UNION Operator in SQL The UNION operator in SQL is used to combine the result set of multiple SELECT statements and return one result set. There are some rules for using the SQL UNION operator. Rules for SQL UNION Each table used within UNION must have th
    3 min read
    SQL BETWEEN Operator
    The BETWEEN operator in SQL is a powerful tool for filtering records within a specific range. Whether applied to numeric, text or date columns it simplifies the process of retrieving data that falls within a particular boundary. In this article, we will explore the SQL BETWEEN operator with examples. SQL BETWEEN OperatorThe SQL BETWEEN operator is
    3 min read
    SQL | BETWEEN & IN Operator
    In SQL, the BETWEEN and IN operators are used for filtering data based on specific criteria. The BETWEEN operator is used to filter results that fall within a specified range of values such as numbers, dates or text while the IN operator is used to filter results based on a list of specific values. In this article, we will see  BETWEEN & IN Ope
    4 min read
    SQL SERVER – Input and Output Parameter For Dynamic SQL
    An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
    3 min read
    Difference between T-SQL and PL-SQL
    1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
    3 min read
    SQL - SELECT from Multiple Tables with MS SQL Server
    In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
    3 min read
    How to Execute SQL Server Stored Procedure in SQL Developer?
    A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
    2 min read
    SQL Query to Check if Date is Greater Than Today in SQL
    In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
    2 min read
    SQL Query to Add a New Column After an Existing Column in SQL
    Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
    3 min read
    SQL Query to Convert Rows to Columns in SQL Server
    In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
    2 min read
    How to SQL Select from Stored Procedure using SQL Server?
    There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
    3 min read
    SQL Quiz : Practice SQL Questions Online
    This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
    3 min read
    BULK INSERT in SQL Server(T-SQL command)
    BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
    3 min read
    SQL Exercises : SQL Practice with Solution for Beginners and Experienced
    SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
    15+ min read
    Difference between SQL and T-SQL
    SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
    4 min read
    SQL Server | Convert Tables in T-SQL into XML
    XML (Extensible Markup Language) is a widely-used markup language designed to store and transfer structured data between different systems and platforms. While HTML focuses on the visual representation of data OverviewXML is similar to HTML which is designed to structure and store data for sharing across different systems and platforms.Unlike HTML,
    3 min read
    RLIKE operator in MySQL
    RLIKE : This operator in MySQL is used to performs a pattern match of a string expression against a pattern. Syntax : RLIKE pattern Parameters : This method accepts one parameter as mentioned in syntax. pattern -The pattern which we want to match against an expression. Various pattern and their usage are described below.PatternWhat the Pattern matc
    2 min read
    MySQL | Operator precedence
    Operator precedence specifies the order in which operators are evaluated when two or more operators with different precedence are adjacent in an expression. For example, 1+2/3 gives the different result as compared to (1+2)/3. Just like all other programming languages C, C++, Java etc. MySQL also has a precedence rule. The following table describes
    3 min read
    PLSQL : || Operator
    The string in PL/SQL is actually a sequence of characters with an optional size specification. The characters could be numeric, letters, blank, special characters or a combination of all. The || Operator in PLSQL is used to concatenate 2 or more strings together. The result of concatenating two character strings is another character string. The res
    2 min read
    Article Tags :