Filtering with SQL IN Operator

The IN operator in SQL is a powerful tool that allows you to filter data by specifying a list of values. Rather than using multiple OR conditions, the IN operator enables you to test whether a value matches any value in a given set. It is efficient and makes queries easier to write and understand when working with multiple matching values.

In this chapter, we will cover the IN operator in detail, starting from the basics to more advanced use cases, including its combination with other SQL clauses. By the end of this chapter, you will have a comprehensive understanding of how to use the IN operator for filtering data.

What is the SQL IN Operator?

The IN operator allows you to specify multiple values in a WHERE clause. It checks if a value matches any value in a list of specified values. This is particularly useful when you need to filter records based on several possible values in a column.

Syntax:

				
					SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

				
			
  • column_name: The column you want to filter.
  • table_name: The table where the data resides.
  • value1, value2, …: A list of values that the column should match.

Basic Usage of the IN Operator

Example 1: Filtering Multiple Values in a Column

Let’s say you have a customers table and want to filter records where the city is either ‘New York’, ‘Los Angeles’, or ‘Chicago’. Using the IN operator simplifies the query.

				
					SELECT name, city
FROM customers
WHERE city IN ('New York', 'Los Angeles', 'Chicago');


				
			

Output:

NameCity
John DoeNew York
Sarah SmithLos Angeles
Mark JohnsonChicago

Explanation:

  • The IN operator checks if the city is one of the three values provided in the list. If it matches any of them, the row is included in the result set.

Example 2: Using IN with Numeric Values

In addition to filtering text data, you can also use IN to filter numeric data. Let’s assume you have an orders table and you want to find orders with IDs 1001, 1005, and 1010.

				
					SELECT order_id, product_name
FROM orders
WHERE order_id IN (1001, 1005, 1010);

				
			

Output:

Order IDProduct Name
1001Laptop
1005Phone
1010Tablet

Combining IN with Other SQL Clauses

Example 3: Using IN with AND Clause

You can combine the IN operator with other SQL clauses such as AND to filter records based on multiple conditions. For instance, let’s filter customers from the cities ‘New York’, ‘Los Angeles’, or ‘Chicago’, but only those who are also over 30 years old.

				
					SELECT name, city, age
FROM customers
WHERE city IN ('New York', 'Los Angeles', 'Chicago')
AND age > 30;

				
			

Output:

NameCityAge
Sarah SmithLos Angeles35
Mark JohnsonChicago40

Explanation:

  • The query retrieves only customers who live in the specified cities and are older than 30.

Example 4: Using IN with NOT Operator

You can negate the IN operator using NOT. This is useful when you want to exclude specific values from the result.

				
					SELECT name, city
FROM customers
WHERE city NOT IN ('New York', 'Los Angeles', 'Chicago');

				
			

Output:

NameCity
Jane WilliamsHouston
Emily DavisMiami

Explanation:

  • This query returns all customers who do not live in ‘New York’, ‘Los Angeles’, or ‘Chicago’.

IN with Subqueries

In more complex cases, you might want to filter data using a subquery. A subquery inside the IN operator can return a list of values that are dynamically generated.

Example 5: IN with Subquery

Suppose you want to find customers who have placed an order in the orders table. Instead of hardcoding customer IDs, you can use a subquery.

				
					SELECT name
FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders);

				
			

Output:

Name
John Doe
Sarah Smith
Mark Johnson

Explanation:

  • The subquery retrieves a list of customer IDs from the orders table, and the outer query filters customers based on those IDs.

Advanced Usage of IN Operator

Example 6: Combining IN with Multiple Columns

The IN operator can also be used with multiple columns by combining it with parentheses and tuples. For example, to find customers who are both from ‘New York’ or ‘Chicago’ and are older than 25, you can write:

				
					SELECT name, city, age
FROM customers
WHERE (city, age) IN (('New York', 28), ('Chicago', 35));

				
			

Output:

NameCityAge
Mark JohnsonChicago35

Explanation:

  • Here, we’re filtering by both city and age in combination.

Example 7: IN with NULL Values

The IN operator behaves slightly differently when dealing with NULL values. In SQL, NULL represents missing or unknown data, and it does not equate to any other value, including itself.

To demonstrate this behavior, let’s try to include a NULL value in an IN list.

				
					SELECT name
FROM customers
WHERE city IN ('New York', NULL);

				
			

Output:

name
John Doe

Explanation:

  • NULL is ignored when using the IN operator. The query only retrieves records where city equals ‘New York’. Rows with NULL in the city field are excluded unless explicitly handled with IS NULL.

Performance Considerations with IN Operator

Although the IN operator is a powerful tool, it can impact performance, especially when dealing with large datasets. To ensure optimal performance:

  • Index Columns: Make sure that the column used with IN is indexed.
  • Limit Large Lists: If you are filtering using a large list of values, consider alternative methods such as using a JOIN with a temporary table.
  • Use Subqueries Wisely: Subqueries can sometimes slow down a query, depending on how the database engine processes them. If performance is an issue, explore rewriting the query using JOIN or EXISTS.

The IN operator is a versatile and efficient way to filter records in SQL. It simplifies queries by allowing you to specify a list of possible values for filtering, reducing the need for complex OR conditions. In addition, the IN operator can be combined with other SQL clauses and subqueries to create more powerful and flexible queries.Throughout this chapter, we explored both basic and advanced usage of the IN operator, including examples with text and numeric data, combinations with other operators, and subqueries. Armed with this knowledge, you should be able to apply the IN operator effectively in a wide range of real-world scenarios. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India