beginner

IN Operator

5 min readLast updated: 2026-07-23

Overview

The IN operator checks if a value matches any entry in a list of literals or a subquery result, replacing long chains of OR clauses.

Learning Objectives

  • Simplify long compound filters using IN and NOT IN.
  • Compare list membership performance and readability advantages.
  • Understand how NULL elements behave inside NOT IN scopes.

Detailed Concept Explanation

Instead of writing repetitive chains of equality checks: WHERE region = 'North' OR region = 'East' OR region = 'West'

You can compress the code cleanly into a set check: WHERE region IN ('North', 'East', 'West')

The NOT IN & NULL Gotcha

If the list inside a NOT IN filter contains a NULL value, the query returns zero rows. This is because col NOT IN (val1, NULL) translates logically to col != val1 AND col != NULL. Since any comparison with NULL returns UNKNOWN, the overall filter can never evaluate to TRUE.


Code Examples

SQL

sql
-- Retrieve active products from specific target categories
SELECT product_name, category 
FROM products 
WHERE category IN ('Electronics', 'Home', 'Office');

-- Retrieve customers not in specific regions
SELECT name, country 
FROM customers 
WHERE country NOT IN ('US', 'CA');

Best Practices

  • List Limits: Keep list constants reasonably sized. For huge sets of parameters, join against a staging table instead of listing thousands of strings in IN().
  • Avoid NULLs in NOT IN: Filter out NULL entries from subqueries before feeding them to a NOT IN filter.

Interview Perspective

Danger

Crucial Trap: NOT IN with NULL Subqueries

An interviewer may ask: "Why does SELECT * FROM users WHERE id NOT IN (SELECT manager_id FROM departments) return zero rows if one manager_id is NULL?"

Answer: Because a single NULL inside NOT IN results in the entire conditional predicate evaluating to UNKNOWN. Resolve by explicitly filtering out NULLs: SELECT manager_id FROM departments WHERE manager_id IS NOT NULL.


Interactive Challenges

Challenge 1: Filter by Department IDs

Select the 'name' and 'salary' of employees who work in department ID 10, 20, or 30.


Summary

The IN operator filters records matching list boundaries. Ensure you guard against NULLs when using NOT IN.