The IN operator in MySQL allows you to filter records by checking whether a column value matches any value in a given list or returned by a subquery. It is commonly used in PHP-MySQL applications to simplify database queries.
IN replaces multiple OR conditionsNOT IN excludes matching values
// Basic syntax of MySQL IN operator
SELECT column_names FROM table_name
WHERE column_name IN (value1, value2, value3);
This example fetches users who belong to selected cities.
// Select users from specific cities
SELECT * FROM users
WHERE city IN ('New York', 'Los Angeles', 'Chicago');
The IN operator can also accept values returned by another query.
// Filter users based on cities from another table
SELECT * FROM users
WHERE city IN (SELECT city FROM offices WHERE department = 'HR');
This query excludes users from certain cities.
// Exclude users from specific cities
SELECT * FROM users
WHERE city NOT IN ('New York', 'Los Angeles');
The above queries return records depending on whether the city column matches (or does not match) values defined inside the IN list or subquery result.
Think of IN as a checklist. If the column value exists in the list, the row is selected — otherwise, it is ignored.
IN over multiple OR conditions for cleaner queriesShirt or PantsLondon, Paris, or Berlin