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
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:
-- Long syntax:
SELECT *
FROM employees
WHERE dept_id = 10 OR dept_id = 20;
You can write concise SQL using IN:
-- Clean syntax with IN:
SELECT *
FROM employees
WHERE dept_id IN (10, 20);
This returns all employees belonging to department 10 OR 20:
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:
SELECT *
FROM employees
WHERE dept_id NOT IN (10, 20);
This filters out all employees in departments 10 and 20, returning only department 30:
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 INcontains aNULL, the entire expression evaluates toUNKNOWN, and the query returns zero rows!
-- 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
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
INtests membership in a set of values or subquery results.NOT INexcludes all values present in the set.- Much cleaner and more readable than multiple chained
OR/ANDstatements. - Beware of
NULLinNOT INlists, which causes queries to return zero rows.
Interview Questions
- What does the IN operator do in SQL?
INdetermines 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 forcolumn = val1 OR column = val2.- How does NOT IN behave compared to AND?
column NOT IN (val1, val2)expands tocolumn != val1 AND column != val2.- What happens if a NOT IN list contains a NULL value?
The condition evaluates to
UNKNOWNfor 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:
- Find all employees who work in department 10 or 30:
SELECT * FROM employees WHERE dept_id IN (10, 30);
- Find all employees who do not work in department 10:
SELECT * FROM employees WHERE dept_id NOT IN (10);
- Rewrite
emp_name = 'John Smith' OR emp_name = 'Emma Brown'usingIN. - 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
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