Lesson 1 of 0
In Progress

Filtering Data with in IN Operators

Kedeisha June 16, 2023

In this lesson, we will explore the usage of the IN and NOT IN operators in SQL for filtering data based on a list of values. These operators allow us to specify multiple values or a subquery as a filtering condition. Understanding how to use the IN and NOT IN operators expands our ability to perform flexible and efficient data filtering in SQL queries.

Using the IN Operator

The IN operator allows us to specify a list of values and check if a column’s value matches any of the values in that list. It is commonly used in the WHERE clause to filter rows based on multiple possible values. The general syntax for using the IN operator is as follows:

				
					SELECT 
    column1, 
    column2, 
FROM table_name
WHERE column IN (value1, value2);

				
			

In the above syntax, “column” represents the column being compared, and “value1, value2, …” represents the list of values to compare against.

Using the NOT IN Operator

The NOT IN operator is the negation of the IN operator. It allows us to exclude rows that match any of the values in the specified list. The general syntax for using the NOT IN operator is as follows:

				
					SELECT 
    column1, 
    column2, 
FROM table_name
WHERE column NOT IN (value1, value2, ...);

				
			

Understanding the IN and NOT IN operators allows us to efficiently filter data based on multiple possible values. They provide a concise way to include or exclude rows that match specific conditions. In the next lesson, we will explore sorting data using the ORDER BY clause to arrange query results in a desired order.