SQL IN Operator
SQL

SQL IN Operator — How to Filter by a List of Values

CHAPTER 17 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL IN operator lets you specify multiple values in a WHERE clause. Instead of writing multiple OR conditions, IN provides a cleaner and shorter way to filter rows that match any value in a list.

SQL IN Syntax

SQL — IN Syntax
SELECT column1, column2
FROM table_name
WHERE column IN (value1, value2, value3, ...);

We will use this Employees table:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

IN with Text Values

SQL
SELECT Name, Department
FROM Employees
WHERE Department IN ('IT', 'HR', 'Sales');
✓ OUTPUT
+-----------+------------+
| Name      | Department |
+-----------+------------+
| Priya K.  | Sales      |
| Arun M.   | IT         |
| Emma W.   | Sales      |
| Ravi S.   | IT         |
| Sara L.   | HR         |
+-----------+------------+

IN vs Multiple OR

These two queries produce the exact same result, but IN is shorter and easier to read:

SQL — Using OR (Verbose)
SELECT Name, City FROM Employees
WHERE City = 'Mumbai'
   OR City = 'Delhi'
   OR City = 'London';
SQL — Using IN (Clean)
SELECT Name, City FROM Employees
WHERE City IN ('Mumbai', 'Delhi', 'London');
✓ OUTPUT (Same for Both)
+-----------+--------+
| Name      | City   |
+-----------+--------+
| Priya K.  | Mumbai |
| John D.   | London |
| Arun M.   | Delhi  |
| Ravi S.   | Mumbai |
+-----------+--------+

IN with Numeric Values

SQL
SELECT Name, Salary FROM Employees
WHERE Salary IN (55000, 62000, 78000);
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Priya K.  | 55000  |
| John D.   | 62000  |
| Arun M.   | 78000  |
+-----------+--------+

NOT IN — Exclude Values

SQL
SELECT Name, City FROM Employees
WHERE City NOT IN ('Mumbai', 'London');
✓ OUTPUT
+-----------+-----------+
| Name      | City      |
+-----------+-----------+
| Arun M.   | Delhi     |
| Emma W.   | Sydney    |
| Sara L.   | New York  |
+-----------+-----------+

⚠️ NOT IN and NULL

Be careful with NOT IN when the list contains NULL. NOT IN (value1, NULL) returns no rows because any comparison with NULL returns UNKNOWN. Always ensure your IN list does not contain NULL values when using NOT IN.

IN with a Subquery

You can use IN with a subquery instead of a static list. The subquery returns a list of values dynamically:

SQL — IN with Subquery
SELECT Name, Department
FROM Employees
WHERE Department IN (
  SELECT Department FROM Employees
  WHERE Salary > 70000
);

This finds employees in departments that have at least one person earning over 70,000. The subquery returns ‘IT’, so all IT employees are shown.

For a complete reference of comparison operators, see the official MySQL comparison operators documentation. In the next chapter, you will learn SQL BETWEEN to filter by a range of values.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that finds employees who work in Mumbai, Sydney, or New York.

ANSWER
SELECT Name, City, Department
FROM Employees
WHERE City IN ('Mumbai', 'Sydney', 'New York');

Expected Output:

+-----------+-----------+------------+
| Name      | City      | Department |
+-----------+-----------+------------+
| Priya K.  | Mumbai    | Sales      |
| Emma W.   | Sydney    | Sales      |
| Ravi S.   | Mumbai    | IT         |
| Sara L.   | New York  | HR         |
+-----------+-----------+------------+

📝 What You Learned in This Chapter

  • IN filters rows matching any value in a list
  • IN is a cleaner alternative to multiple OR conditions
  • Works with text values, numbers, and subqueries
  • NOT IN excludes rows matching values in the list
  • Be careful with NOT IN when the list might contain NULL

Frequently Asked Questions

What does the IN operator do in SQL?
The IN operator checks if a value matches any value in a list. It is shorthand for multiple OR conditions. WHERE City IN ('Mumbai', 'Delhi') is the same as WHERE City = 'Mumbai' OR City = 'Delhi'.
Is IN faster than multiple OR conditions?
In most databases, IN and multiple OR conditions perform the same because the query optimizer converts them into the same execution plan. However, IN is preferred for readability and maintainability, especially when the list is long.
Can I use IN with a subquery?
Yes. WHERE column IN (SELECT column FROM other_table) filters rows based on values returned by a subquery. This is powerful for dynamic filtering where the list of values comes from another table or calculation.
What happens with NOT IN and NULL?
NOT IN returns no rows if the list contains NULL. This is because value <> NULL evaluates to UNKNOWN, which makes the entire NOT IN condition UNKNOWN. Always filter out NULLs from subqueries used with NOT IN.
How many values can I put in an IN list?
There is no fixed SQL standard limit. MySQL supports thousands of values. SQL Server limits to about 2,100 parameters. Oracle allows up to 1,000 items. For very large lists, consider using a temporary table and JOIN instead of IN.

📖 Chapter 17 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com