Introduction to MySQL IN Operator
- MySQL IN Operator used to retrieve the result set by checking whether a value from the set of literal values or provided by the subquery is matched or not.
- Basically, this operator helps to determine the specific value if available in the result of subquery using the WHERE clause.
- It fetches the set of columns from the table if a particular value is present in the list of values provided to match a conditional expression.
- Thus, the MySQL IN operator like other MySQL operators allows comparing a value matching from the set of column values returned by a subquery statement.
- If we want to get the rows as result from the Database table where a particular value should be present in a list of values available through conditional query then, we use MySQL IN operator.
- We extract the rows which have matching values in the list of values in IN operator function from a particular value of column given using WHERE keyword in MySQL.
We have the following SQL syntax statement to illustrate the IN operator working and query in MySQL:
SELECT * FROM TableName WHERE (ColumnA) IN (SELECT query statement);
SELECT ColumnA, ColumnB,.., FROM TableName WHERE (ColumnA or any expressional condition) IN (‘ValueA’, ‘ValueB’,…..);
Explaining the above syntax in brief:
- We need to provide any column value as ColumnA or use any expression with IN operator in the query of WHERE clause.
- In the IN operator list, we need to differentiate the values with a comma (,).
- We can provide any subquery statement using the SELECT clause in MySQL with IN keyword together to filter the values from other tables and run the statement to get the required set of result rows from the table.
- The result of IN operator will be 1 if the column value or the subquery expression result value matches any values present in the list of values. If it does not match then, the result of IN operator will be 0.
How IN Operator Works in MySQL?
As per the above syntax, MySQL IN operator provides us an equal value in the arguments. To use the IN Keyword it is combined with WHERE clause together. When this happens, the query executed only affects the records whose values are matched with the list of values available in the IN Keyword. IN keyword in MySQL helps to minimize the use of the OR clause.
Suppose we have all the constant values in the list, then in MySQL following operational steps are carried:
- Firstly, it examines if the values are of the same data type as of the ColumnA or not. Also, evaluation is done for expression or subquery results.
- Next, the values are sorted in a proper sequence.
- Then, the value is searched based on the Binary Search Algorithm, which allows the SQL statement query using IN operator with a list of constant values to execute very speedily.
- Otherwise, based on the rules in MySQL type conversion can take place to process the query.
Examples to Implement IN operator in MySQL
Let us now discuss the IN operator using some examples and learn the code to write the SQL query for it.
Example #1 – within, NOT IN and OR
We are using tables named ‘Customers’ and ‘Employees’. Below is the SQL statement using IN operator in the Customers table.
SELECT * FROM Customers WHERE City IN (‘Delhi’, ‘Mumbai’);
The above query with IN operator will fetch all the records from the table where the customers are from the cities provided in the IN keyword: Delhi and Mumbai.
If we use OR operator in the query above then, the result will be the same as above:
SELECT * FROM Customers WHERE City = ‘Delhi’ OR City = ‘Mumbai’;
But if we have multiple values in the list, then we need to separate each value with OR operators and the statement will be constructed very long. Therefore, to avoid such kind of issue and making the SQL syntax more proper and readable, the IN operator is introduced in MySQL. So, the IN keywords has helped to shorten the statement by listing values together separated by commas.
In reverse, if we want to get the result set which does not match the values in the list then, we use the following query with NOT IN operator in MySQL:
SELECT * FROM Customers WHERE City NOT IN (‘Delhi’, ‘Mumbai’);
You can see from the result above that NOT IN operator has fetched the rows of customers form the table that does not belong or related to the cities provided in the list: Delhi and Mumbai.
Example #2 – With Subquery
Generally, the IN operator is used with an inner query or subquery. It is done so because instead of passing a list of literal values with IN keyword, we can use a subquery which fetches the list of values from one or multiple tables and provides them as input to IN operator to match the value in the outer query and produce the result from the SQL statement.
In the following query, we have taken both tables Customers (CustNum, Name, City, Phone) and Employees (EmpNum, Profile, CustNum, Salary) to filter the result set from both the tables in MySQL:
SELECT CustNum,Name, City FROM Customers WHERE CustNum IN (SELECT CustNum FROM Employees where EmpNum > 202 ) ORDER BY CustNum;
Here, the IN Operator has provided the result rows from Customers using subquery where the output of subquery from Employees is the values of the CustNum column which includes the employees having their salary greater than 5000.
- Remember that as per the above IN operator syntax, if ColumnA value is not present in the table or subquery expression provides NULL value or if any values in the list are NULL, then the condition does not give proper comparability of values and thus, the result of IN operator will be NULL.
- If we want to get a result where the value does not match the values in the list of values then, we can use NOT IN operator instead of IN.
- Hence, along with the WHERE clause we can use IN operator in MySQL in other SQL queries or statements like UPDATE and DELETE.
This is a guide to MySQL IN Operator. Here we discuss the Introduction to MySQL IN Operator and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –