How can I get the output based on comparison done with column’s name using MySQL IN() function?


In this scenario, we need to use the name of the column as ‘Expression’ which will then be compared with the values in the list. If a column has value/s matched within the list, the output would be produced. For understanding it, consider the example from employee table having the following data −

mysql> Select * from Employee;
+----+--------+--------+
| ID | Name   | Salary |
+----+--------+--------+
| 1  | Gaurav | 50000  |
| 2  | Rahul  | 20000  |
| 3  | Advik  | 25000  |
| 4  | Aarav  | 65000  |
| 5  | Ram    | 20000  |
| 6  | Mohan  | 30000  |
+----+--------+--------+
6 rows in set (0.00 sec)

Now, we can use column ‘ID’ with IN() function as follows −

mysql> Select * from Employee WHERE ID IN(6,2,3,20,10,9);
+----+-------+--------+
| ID | Name  | Salary |
+----+-------+--------+
| 2  | Rahul | 20000  |
| 3  | Advik | 25000  |
| 6  | Mohan | 30000  |
+----+-------+--------+
3 rows in set (0.00 sec)

From the above result set, it is clear that IN() function matches the value of column ‘ID’ with the values in the list and gives the rows as output for which it matched.

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 22-Jun-2020

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements