MySQL IN
Summary: in this tutorial, you will learn how to use MySQL IN operator to determine if a specified value matches any value in a list of values.
Introduction to the MySQL IN operator
The IN operator allows you to determine if a value matches any value in a list of values. Here’s the syntax of the IN operator:
value IN (value1, value2, value3. )
Code language: SQL (Structured Query Language) (sql)
The IN operator returns 1 (true) if the value equals any value in the list ( value1 , value2 , value3 ,…). Otherwise, it returns 0.
- First, specify the value to test on the left side of the IN operator. The value can be a column or an expression.
- Second, specify a comma-separated list of values to match in the parentheses.
The IN operator is functionally equivalent to the combination of multiple OR operators:
value = value1 OR value = value2 OR value = value3 OR .
Code language: SQL (Structured Query Language) (sql)
The following example returns 1 because 1 is in the list:
+--------------+ | 1 IN (1,2,3) | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec)
Code language: plaintext (plaintext)
The following example returns 0 because 4 is not in the list:
+--------------+ | 4 IN (1,2,3) | +--------------+ | 0 | +--------------+ 1 row in set (0.00 sec)
Code language: plaintext (plaintext)
In practice, you’ll use the IN operator to form conditions in a WHERE clause of the SELECT , DELETE , and UPDATE statements. Also, you’ll use the IN operator in a query that contains a subquery.
MySQL IN operator and NULL
Generally, the IN operator returns NULL in two cases:
- The value on the left side of the operator is NULL.
- The value doesn’t equal any value in the list and one of values in the list is NULL.
The following example returns NULL because the value of the left side of the IN operator is NULL:
SELECT NULL IN (1,2,3);
Code language: PHP (php)
+-----------------+ | NULL IN (1,2,3) | +-----------------+ | NULL | +-----------------+ 1 row in set (0.00 sec)
Code language: plaintext (plaintext)
The following example also returns NULL because the 0 is not equal to any value in the list and the list has one NULL:
SELECT 0 IN (1 , 2, 3, NULL);
Code language: PHP (php)
+-----------------------+ | 0 IN (1 , 2, 3, NULL) | +-----------------------+ | NULL | +-----------------------+ 1 row in set (0.00 sec)
Code language: plaintext (plaintext)
The following example also returns NULL because NULL is not equal to any value in the list and the list has one NULL. Note that NULL is not equal to NULL.
SELECT NULL IN (1 , 2, 3, NULL);
Code language: PHP (php)
MySQL IN operator examples
See the following offices table from the sample database:
The following example uses the IN operator to find the offices located in the USA and France:
SELECT officeCode, city, phone, country FROM offices WHERE country IN ('USA' , 'France');
Code language: SQL (Structured Query Language) (sql)
+------------+---------------+-----------------+---------+ | officeCode | city | phone | country | +------------+---------------+-----------------+---------+ | 1 | San Francisco | +1 650 219 4782 | USA | | 2 | Boston | +1 215 837 0825 | USA | | 3 | NYC | +1 212 555 3000 | USA | | 4 | Paris | +33 14 723 4404 | France | +------------+---------------+-----------------+---------+ 4 rows in set (0.01 sec)
Code language: plaintext (plaintext)
You can also get the same result with the OR operator like this:
SELECT officeCode, city, phone FROM offices WHERE country = 'USA' OR country = 'France';
Code language: SQL (Structured Query Language) (sql)
In case the list has many values, you need to construct a very long statement with multiple OR operators. Hence, the IN operator allows you to shorten the query and make it more readable.
Summary
- Use the IN operator to check if a value is in a set of values.
- Use the IN operator to form a condition for the WHERE clause.
MySQL IN() function
MySQL IN() function finds a match in the given arguments.
The function returns 1 if expr is equal to any of the values in the IN list, otherwise, returns 0. If all values are constants, they are evaluated according to the type of expr and sorted. The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants. Otherwise, type conversion takes place according to the rules.
MySQL Version: 5.6
Example: MySQL IN() function
The following MySQL statement will return 1 because the specified value is within the range of values.
Sample Output:
mysql> SELECT 10 IN(15,10,25); +-----------------+ | 10 IN(15,10,25) | +-----------------+ | 1 | +-----------------+ 1 row in set (0.00 sec)
Example : IN() function with where clause
The following MySQL statement checks which books have either 300 or 400 or 500 pages.
SELECT book_name,dt_of_pub,no_page FROM book_mast WHERE no_page IN (300,400,500);
Relational Algebra Expression:
Relational Algebra Tree:
Sample table: book_mast
Sample Output:
mysql> SELECT book_name,dt_of_pub,no_page -> FROM book_mast -> WHERE no_page IN (300,400,500); +-------------------------------------+------------+---------+ | book_name | dt_of_pub | no_page | +-------------------------------------+------------+---------+ | Understanding of Steel Construction | 2003-07-15 | 300 | | Fundamentals of Thermodynamics | 2002-10-14 | 400 | +-------------------------------------+------------+---------+ 2 rows in set (0.09 sec)
Book Date of publish Number of pages query('SELECT book_name,dt_of_pub,no_page FROM book_mast WHERE no_page IN (300,400,500)') as $row) < echo ""; echo "" . $row['book_name'] . " "; echo "" . $row['dt_of_pub'] . " "; echo "" . $row['no_page'] . " "; echo " "; > ?>