The MySQL IN operator is used to filter data based on a specified set of values. It is a shorthand for multiple OR conditions which allows us to specify a list of values in a WHERE clause to match records that have any of those values. This makes your SQL queries more concise and easier to read.
MySQL IN Operator
The IN operator is a powerful and efficient way to filter data within a specified set of values. It is particularly useful when we need to match a column's value against multiple possible values. Instead of writing multiple OR conditions, we can use the IN operator to check multiple values. It can be used with any data type.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
Parameters:
- column1, column2, ...: The columns to retrieve data from.
- table_name: The name of the table from which to retrieve data.
- column_name: The column to compare against the list of values.
- value1, value2, ...: The list of values to check against.
Demo MySQL Database
We will create a sample database to see how to use the IN operator.
Create 'studentsInfo' table:
CREATE TABLE studentsInfo (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade CHAR(2),
city VARCHAR(50)
);
INSERT INTO studentsInfo (name, age, grade, city) VALUES
('Amit Sharma', 18, 'A', 'Delhi'),
('Priya Singh', 19, 'B', 'Mumbai'),
('Raj Patel', 20, 'A', 'Ahmedabad'),
('Sneha Reddy', 21, 'C', 'Hyderabad'),
('Arjun Rao', 22, 'B', 'Bangalore');
Output
id | name | age | grade | city |
---|
1 | Amit Sharma | 18 | A | Delhi |
2 | Priya Singh | 19 | B | Mumbai |
3 | Raj Patel | 20 | A | Ahmedabad |
4 | Sneha Reddy | 21 | C | Hyderabad |
5 | Arjun Rao | 22 | B | Bangalore |
MySQL IN Operator Example
Let’s see some examples of IN Operator and understand it works in MySQL:
Example 1: Select students who are in grades 'A' or 'B'
SELECT id, name, grade, city
FROM studentsInfo
WHERE grade IN ('A', 'B');
Output:
id | name | grade | city |
---|
1 | Amit Sharma | A | Delhi |
2 | Priya Singh | B | Mumbai |
3 | Raj Patel | A | Ahmedabad |
5 | Arjun Rao | B | Bangalore |
Explanation: This query retrieves students whose grades are either 'A' or 'B'. The IN
operator checks if the grade
column's value matches 'A' or 'B'.
Example 2: Select students whose city is not Mumbai or Bangalore
SELECT id, name, city
FROM studentsInfo
WHERE city NOT IN ('Mumbai', 'Bangalore');
Output:
id | name | city |
---|
1 | Amit Sharma | Delhi |
3 | Raj Patel | Ahmedabad |
4 | Sneha Reddy | Hyderabad |
Explanation: This query retrieves students whose cities are neither 'Mumbai' nor 'Bangalore'. The NOT IN
operator checks if the city
column's value does not match 'Mumbai' or 'Bangalore'.
Example 3: Select students aged 18, 20, or 22
SELECT id, name, age, city
FROM studentsInfo
WHERE age IN (18, 20, 22);
Output:
id | name | age | city |
---|
1 | Amit Sharma | 18 | Delhi |
3 | Raj Patel | 20 | Ahmedabad |
5 | Arjun Rao | 22 | Bangalore |
Explanation: This query retrieves students whose ages are either 18, 20, or 22. The IN
operator checks if the age
column's value matches any of the specified values (18, 20, 22).
Conclusion
The MySQL 'IN' operator is a powerful tool for filtering data by matching column values against a specified set. It simplifies complex queries and enhances readability, making it easier to manage and analyze data. By effectively utilizing the IN operator, you can simplify your SQL queries, improve performance, and handle multiple conditions with ease, making database management more efficient.
Similar Reads
MySQL LIKE Operator
The MySQL LIKE operator helps us search for specified patterns in a column. It is useful when we need to find records that match specific patterns, like names starting with a certain letter or containing a specific word. In this article, we will cover the concept, syntax, and examples of using the L
3 min read
MySQL EXISTS Operator
The EXISTS operator in MySQL is a powerful boolean operator used to test the existence of any record in a subquery. It returns true if the subquery yields one or more records, enabling efficient data retrieval and manipulation, particularly in large datasets. The operator is often paired with subque
6 min read
MySQL BETWEEN Operator
MySQL consists of various operators for performing efficient data queries, and the BETWEEN operator is a key operator for filtering records within a specific range. By specifying a lower and upper bound, BETWEEN helps you easily retrieve data that falls between two values. This operator simplifies q
4 min read
MySQL EXCEPT Operator
Comparison of two different sets of data in a database and finding entries unique to one set is one of the common operations done on databases. The EXCEPT operator in SQL compares the two result sets returned and returns only the rows that are found in one but not the other.Moreover, MySQL, one of t
6 min read
MySQL UNION Operator
Database management is an important concept of handling and organizing data effectively. One powerful Operator in MySQL for combining results from multiple queries is the UNION operator. This operator allows you to merge the results of two or more SELECT statements into a single result set, eliminat
4 min read
MySQL NOT EQUAL Operator
SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. It provides a standardized language for querying the databases which are found to be structured, allowing users to define, manipulate, and control the data retrieval with ease. SQL operate
4 min read
MySQL ANY and ALL Operators
When working with databases, there are often scenarios where we need to compare a value against multiple other values. MySQL offers two powerful operators for this purpose such as ANY and ALL Operators. These operators allow for more complex and flexible data retrieval, enabling comparisons between
4 min read
MySQL UNION ALL Operator
The UNION ALL operator in MySQL combines the result sets of multiple SELECT statements by retaining all duplicate rows for improved performance and efficiency. It is particularly useful when complete data inclusion, including duplicates is required.In this article, We will learn about the MySQL UNIO
4 min read
MongoDB $in Operator
MongoDB $in operator provides a powerful way to query documents based on multiple potential values for a specific field within a single query.In this article, We will learn about the MongoDB $in Operator by understanding various examples in detail.MongoDB $in OperatorThe MongoDB $in operator is used
4 min read
SQL IN Operator
The SQL IN operator filters data based on a list of specific values. In general, we can only use one condition in the Where clause, but the IN operator allows us to specify multiple values. In this article, we will learn about the IN operator in SQL by understanding its syntax and examples.IN Operat
4 min read