Checking your session…
Module 08: Data Filtering and Operators

8.4 IN / NOT IN

Level: 8 | Version: v1.1 | Author: Meptrasoft

Overview

The IN operator allows you to easily specify multiple values in a WHERE clause.

Instead of chaining multiple OR conditions, IN provides a clean and compact way to match any value against a list.

Operator Purpose Equivalent Condition
IN Matches any value in a specified list Chained OR (col = A OR col = B)
NOT IN Excludes all values in a specified list Chained AND != (col != A AND col != B)

IN → Matches ANY value in the list
NOT IN → Excludes ALL values in the list

SQL IN vs NOT IN — Multi-Value Filtering IN replaces multiple OR statements; NOT IN excludes all listed values EMPLOYEES John Smithdept 10 Emma Browndept 20 Samuel Reeddept 10 Andersondept 30 Sarah Wilsondept 20 Tom Watsondept 10 WHERE dept_id IN (10, 20) dept_id = 10 OR dept_id = 20 ✓ Matches dept 10 (John, Samuel, Tom) ✓ Matches dept 20 (Emma, Sarah) ✗ Excludes dept 30 (Anderson) WHERE dept_id NOT IN (10, 20) dept_id != 10 AND dept_id != 20 ✓ Matches dept 30 (Anderson) ✗ Excludes all rows in dept 10 and 20 MEMORY RULE: IN = Multiple OR Conditions | NOT IN = Multiple AND != Conditions
Figure 1: IN matches any value specified in a list, while NOT IN excludes all values in the list.

Practice Table

This topic reuses the employees table from 8.1 The WHERE Clause.

emp_id emp_name dept_id salary
1 John Smith 10 45000
2 Emma Brown 20 60000
3 Samuel Reed 10 32000
4 Anderson 30 55000
5 Sarah Wilson 20 48000
6 Tom Watson 10 51000

Using IN

Instead of writing verbose multiple OR statements:

SQL
-- Long syntax:
SELECT *
FROM employees
WHERE dept_id = 10 OR dept_id = 20;

You can write concise SQL using IN:

SQL
-- Clean syntax with IN:
SELECT *
FROM employees
WHERE dept_id IN (10, 20);

This returns all employees belonging to department 10 OR 20:

TEXT
emp_id | emp_name     | dept_id | salary
1      | John Smith   | 10      | 45000
2      | Emma Brown   | 20      | 60000
3      | Samuel Reed  | 10      | 32000
5      | Sarah Wilson | 20      | 48000
6      | Tom Watson   | 10      | 51000

Using NOT IN

NOT IN returns rows where the column value is not present in the specified list:

SQL
SELECT *
FROM employees
WHERE dept_id NOT IN (10, 20);

This filters out all employees in departments 10 and 20, returning only department 30:

TEXT
emp_id | emp_name | dept_id | salary
4      | Anderson | 30      | 55000

NOT IN Equivalent: dept_id != 10 AND dept_id != 20


Critical Caveat: NOT IN with NULL Values

[!WARNING] If the list in NOT IN contains a NULL, the entire expression evaluates to UNKNOWN, and the query returns zero rows!

SQL
-- Returns ZERO rows because (10, NULL) causes UNKNOWN for every comparison:
SELECT * FROM employees WHERE dept_id NOT IN (10, NULL);

Why? Because dept_id != 10 AND dept_id != NULL. Since != NULL is always UNKNOWN, TRUE AND UNKNOWN produces UNKNOWN, which fails the WHERE condition.


Common Beginner Mistakes

✗ Mistake ✓ Correct Understanding
Writing WHERE dept_id = (10, 20) Use IN (10, 20) when comparing against multiple values
Assuming NOT IN works like OR NOT IN (10, 20) means != 10 AND != 20
Including NULL inside NOT IN (...) A single NULL in NOT IN invalidates the entire filter
Using string quotes for integers (IN ('10', '20')) Match data types; numbers should not be enclosed in quotes

Placement Quick Points

TEXT
IN (A, B, C)
→ col = A OR col = B OR col = C

NOT IN (A, B, C)
→ col != A AND col != B AND col != C
  • IN tests membership in a set of values or subquery results.
  • NOT IN excludes all values present in the set.
  • Much cleaner and more readable than multiple chained OR / AND statements.
  • Beware of NULL in NOT IN lists, which causes queries to return zero rows.

Interview Questions

What does the IN operator do in SQL?

IN determines whether a specified value matches any value in a list or subquery.

How is IN related to the OR operator?

column IN (val1, val2) is shorthand syntactic sugar for column = val1 OR column = val2.

How does NOT IN behave compared to AND?

column NOT IN (val1, val2) expands to column != val1 AND column != val2.

What happens if a NOT IN list contains a NULL value?

The condition evaluates to UNKNOWN for all rows, resulting in an empty query result set (zero rows returned).

Can IN be used with subqueries?

Yes! WHERE dept_id IN (SELECT dept_id FROM departments WHERE budget > 100000) is standard SQL.


Practice & Hands-On Exercises

Using the employees table:

  1. Find all employees who work in department 10 or 30:
SQL
SELECT * FROM employees WHERE dept_id IN (10, 30);
  1. Find all employees who do not work in department 10:
SQL
SELECT * FROM employees WHERE dept_id NOT IN (10);
  1. Rewrite emp_name = 'John Smith' OR emp_name = 'Emma Brown' using IN.
  2. Test what happens when you run SELECT * FROM employees WHERE dept_id NOT IN (10, NULL);.

💡 Tip: Test your queries using the in-browser interactive runner above.


Key Takeaway

TEXT
IN     → MATCH ANY (OR)
NOT IN → EXCLUDE ALL (AND !=)

Use IN to match against multiple candidate values cleanly, and ensure NOT IN lists do not contain NULL values.

End of IN / NOT IN