The IN operator is used in a WHERE clause to filter records that match any value in a given list.
👉 It is used when you want to compare a column with multiple possible values.
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, value3, ...);
Instead of writing multiple OR conditions:
column_name = value1 OR column_name = value2 OR column_name = value3
👉 You can write:
column_name IN (value1, value2, value3)
| ID | Name | Age |
|---|---|---|
| 1 | Amit | 18 |
| 2 | Riya | 21 |
| 3 | Rahul | 19 |
| 4 | Neha | 23 |
| 5 | Karan | 20 |
SELECT * FROM Students
WHERE Age IN (18, 20, 23);
Returns all rows where Age is either 18, 20, or 23.
SELECT * FROM Students
WHERE Name IN ('Amit', 'Neha');
Returns rows where Name matches any value in the list.
IN can also be used with a subquery (a query inside another query).
SELECT * FROM Students
WHERE ID IN (SELECT ID FROM GraduatedStudents);
Used to exclude values from a list.
SELECT * FROM table_name
WHERE column_name NOT IN (value1, value2);
SELECT * FROM Students
WHERE Age NOT IN (18, 21);
Returns students whose age is not 18 or 21.
Age IN ('18', '20') -- ❌ Wrong (strings instead of numbers)
Age IN (18, 20) -- ✅ Correct
WHERE Name IN ('Amit', 'Riya')
👉 Same as:
WHERE Name = 'Amit' OR Name = 'Riya'
IN does not match NULL values.IS NULL.| Operator | Meaning |
|---|---|
| IN | Matches any value in a list |
| NOT IN | Excludes values in a list |
The IN operator: