The MySQL IN
operator is used to check whether a value matches any value in a list of values. It is commonly used in the WHERE
clause of a SELECT
statement to filter results based on a set of specified values.
The syntax of the IN
operator is as follows:
SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ..., valueN);
In this syntax, column_name
is the name of the column you want to check, table_name
is the name of the table, and value1
, value2
, …, valueN
are the values you want to check for.
Here’s an example of how you could use the IN
operator to select data from a table:
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing', 'Customer Service');
In this example, the IN
operator is used to select all employees who work in the Sales, Marketing, or Customer Service departments.
Note that the list of values in the IN
operator can also be a subquery that returns a list of values. For example:
SELECT * FROM employees WHERE department IN (SELECT department_name FROM departments WHERE region = 'West');
In this example, the IN
operator is used with a subquery to select all employees who work in departments located in the West region.