SQL IN Operator
beginnerfiltering
2 min read
What is the IN Operator?
The IN operator checks whether a value matches any value in a list. It is a concise alternative to multiple OR conditions.
Syntax
SELECT * FROM table_name
WHERE column IN (value1, value2, value3);With a subquery:
SELECT * FROM table_name
WHERE column IN (SELECT column FROM other_table);When to Use
- Filtering by a set of known values (status codes, category names, IDs)
- Replacing long chains of
ORconditions - Checking membership against a subquery result
Key Points
- Readability —
WHERE id IN (1, 2, 3)is cleaner thanWHERE id = 1 OR id = 2 OR id = 3. - NOT IN — Use
NOT INto exclude values. Beware: if the list contains NULL,NOT INreturns no rows. - Subqueries —
IN (SELECT ...)dynamically generates the list from another query. - Performance — For very large lists, consider using a JOIN or temporary table instead.
- Data Types — Ensure the list values match the column's data type to avoid implicit casts.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL IN Operator Challenge
Write a query that find students with grade A or B.
Expected result
Students whose grade is either A or B.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is the IN Operator?
Initializing database...Each run starts from fresh sample data.
More Examples
IN with a subquery
Find students enrolled in any course.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
What happens if the IN list contains NULL?
With IN, NULLs are harmless; rows matching other values still return. With NOT IN, a NULL in the list causes the entire condition to return no rows. Use NOT EXISTS instead.
Is IN faster than multiple OR conditions?
They are typically equivalent in performance. The query planner optimizes both the same way. IN is preferred for readability.