MySQL IN Operator
MySQL IN Operator
The IN operator in MySQL is used in the WHERE clause to specify multiple values in a condition. It is a shorthand for using multiple OR conditions.
✅ Syntax
-
Returns all rows where the column value matches any value in the list.
🧠 Example Table: students
| id | name | age | city |
|---|---|---|---|
| 1 | John | 20 | New York |
| 2 | Emma | 21 | Chicago |
| 3 | Raj | 22 | Mumbai |
| 4 | Arjun | 23 | Delhi |
| 5 | Sara | 19 | London |
✅ Example 1: Select Specific Cities
✔ Returns:
| id | name | age | city |
|---|---|---|---|
| 1 | John | 20 | New York |
| 4 | Arjun | 23 | Delhi |
| 5 | Sara | 19 | London |
✅ Example 2: Select Multiple Ages
✔ Returns students with age 20 or 22:
| id | name | age | city |
|---|---|---|---|
| 1 | John | 20 | New York |
| 3 | Raj | 22 | Mumbai |
✅ Example 3: NOT IN
You can also exclude specific values using NOT IN:
✔ Returns students not in New York or London:
| id | name | age | city |
|---|---|---|---|
| 2 | Emma | 21 | Chicago |
| 3 | Raj | 22 | Mumbai |
| 4 | Arjun | 23 | Delhi |
🧠 Benefits of Using IN
-
Simplifies queries compared to multiple OR conditions:
-
Makes queries cleaner and more readable.
✅ Summary
| Operator | Purpose |
|---|---|
| IN | Match any value in a list |
| NOT IN | Exclude values in a list |
