MySQL IN Operator

MySQL | IN Operator: Learn about the MySQL IN Operator, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 13, 2022

IN Operator

IN operator helps you to match some specific values of the column. It sounds confusing, but this operator aids us very much when it comes to write big queries, as we can even match some specific query inside a query, which sounds even more confusing. So in simple words, IN operator helps us to match the values of the column.

IN Operator Syntax

SELECT column_name1, Column_name2, ...
FROM table_name
WHERE column_name IN (values);

Let us see some examples;

IN Operator Examples

Suppose we have a table named "student_details" and inside that table, we have the following columns,

IN Operator Example 1

Case 1: Now, suppose if I want to select all the details of the table "student_details" which have ("CSE", "ME") branch, in such case, we can simply write,

SELECT * FROM student_details 
WHERE student_department IN ('CSE','ME');
IN Operator Example 2

As you can see in the result given above, we have all the details of the student of department "CSE", "ME".

Now, the question arises that we could have done this task using AND operator also then why this new operator? You can complete this task using AND operator but for that, you need to write a long statement, and here as you can see how easily we completed this.

Case 2: Suppose we want to know the details of the male students of the "CSE" and "ME" departments, then by using IN operator our query will be,

SELECT * FROM student_details 
WHERE student_department IN ('CSE','ME') AND gender IN ('Male');
IN Operator Example 3

Here in this example, I have used two IN operators and I have separated them with AND operator this is because I want both conditions to be fulfilled.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.