Learn SQL
-- filtering.in_not_in

IN / NOT IN in SQL

Match against a list of values

--concept

IN tests a column against a comma-separated list — department IN ('Sales', 'Marketing') is exactly department = 'Sales' OR department = 'Marketing', just shorter and faster to read. NOT IN flips it. The list can also come from a subquery, making IN the workhorse of semi-joins.

--how it works
1

List the candidates

department IN ('Sales', 'Marketing') — values in parentheses, comma-separated, quotes for text.

2

It is sugar for ORs

Exactly department = 'Sales' OR department = 'Marketing' — same plan, far less typing, harder to misplace a parenthesis.

3

NOT IN flips it

Keeps every row outside the list.

4

The list can be a query

WHERE dept_id IN (SELECT dept_id FROM active_departments) — a semi-join in disguise.

5

Respect the NULL trap

If the list contains NULL, NOT IN returns zero rows — filter NULLs out first or use NOT EXISTS.

--visualization
idnamedepartment1AliceEngineering2BobSales3CynthiaEngineering4DavidMarketing5ElenaSales6FaridEngineeringdepartment IN (Engineering, Marketing)
kept dropped
Stage 1 / 8
WHERE department IN (Engineering, Marketing)
--example
query.sql
1SELECT name, department
2FROM employees
3WHERE department IN ('Sales', 'Marketing');
Result
namedepartment
BobSales
DavidMarketing
ElenaSales

Three rows match the two-item list — same result as the OR chain, one third of the typing. Tick departments in/out of the list in the visualization.

--variants

IN with a list

Membership over an OR chain.

query.sql
1SELECT name, department
2FROM employees
3WHERE department IN ('Sales', 'Marketing');
Result
namedepartment
BobSales
DavidMarketing
ElenaSales
1Each row checks membership against the two-item set.
2Three employees belong; three do not.
3Adding 'Engineering' to the list would keep everyone.

NOT IN

The complement of membership.

query.sql
1SELECT name, department
2FROM employees
3WHERE department NOT IN ('Sales', 'Marketing');
Result
namedepartment
AliceEngineering
CynthiaEngineering
FaridEngineering
1The three Engineering rows are outside the list.
2NOT IN (list without NULL) is safe and exact.
3Same result as two <>s ANDed — but reads in one breath.

Numbers in a list

IDs and codes, unquoted.

query.sql
1SELECT name FROM employees
2WHERE id IN (1, 3, 6);
Result
name
Alice
Cynthia
Farid
1The set {1, 3, 6} is checked per row.
2Exactly the three Engineering ids — coincidence of our data, not a rule.
3This is the "batch lookup by ids" pattern every app uses.

NOT IN + NULL = zero rows

The famous three-valued-logic ambush.

query.sql
1SELECT name FROM employees
2WHERE department NOT IN ('Sales', NULL);
3-- returns NOTHING, not the non-Sales rows!
1NOT IN must prove department differs from EVERY list item.
2dept = 'Engineering' vs NULL is UNKNOWN — proof impossible.
3Result: zero rows. Filter NULLs first or use NOT EXISTS.
--key facts
Readabilityone IN beats four ORs — and optimises identically
NULL trapNOT IN (list containing NULL) matches nothing at all
Power moveIN (SELECT …) links tables without a JOIN
--use cases
Batch lookups: WHERE id IN (…) after collecting ids in application code
Category filters: type IN ('debit', 'credit')
Subquery membership: users IN (SELECT user_id FROM banned)
Teaching moment for NOT IN + NULL — a top-three interview trap
--under the hood
Engines turn IN lists into a hash probe per row — cheap and index-friendly.
Long IN lists from application loops often mean you wanted a JOIN instead; keep lists in the hundreds, not tens of thousands.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.