IN
The IN
operator allows you to determine if a specified value matches any value in a set of values or returned by a subquery.
The following illustrates the syntax of the IN
operator:
Let’s examine the query in more detail:
Use a
column
or an expression (expr
) with theIN
operator in theWHERE
clause.Separate the values in the list by commas (,).
The IN
operator returns 1 if the value of the column_1
or the result of the expr
expression is equal to any value in the list, otherwise, it returns 0.
When the values in the list are all constants, MySQL performs the following steps:
First, evaluate the values based on the type of the
column_1
or result of theexpr
expression.Second, sort the values.
Third, search for the value using the binary search algorithm. Therefore, a query that uses the
IN
operator with a list of constants performs very fast.
Note that if the expr
or any value in the list is NULL
, the IN
operator returns NULL
.
You can combine the IN
operator with the NOT
operator to determine if a value does not match any value in a list or a subquery. And you can also use the IN
operator in the WHERE
clause of other statements such as UPDATE
, and DELETE
.
If you want to find the offices that locate in the U.S. and France, you can use the IN
operator as the following query:
You can achieve the same result with the OR
operator as the following query:
Last updated