MySQL Tutorial : MySQL IN clause (Basic)

MySQL IN clause can be used instead of using OR operator with Where clause. It can be used when where clause is applied for multiple values of the same column.

To understand IN clause, consider user_table table, which has the following records:

mysql> SELECT * FROM user_table;
+------+------+------------+--------------------+
| id   | name | join_date  |    no_of_posts     |
+------+------+------------+--------------------+
|    1 | Jim  | 2013-01-24 |                 50 |
|    2 | Rambo| 2013-05-27 |                 20 |
|    3 | Jack | 2013-05-06 |                 70 |
|    4 | Bill | 2013-04-06 |                 20 |
|    5 | Tara | 2013-06-06 |                 30 |
+------+------+------------+--------------------+
5 rows in set (0.00 sec)

Let’s say you want to show records with no_of_posts equal to 50 and 20 and 70. This can be done using OR conditions as follows

mysql>SELECT * FROM user_table 
    ->WHERE no_of_posts= 50 OR  
    ->no_of_posts= 20 OR  no_of_posts= 70; 
+------+------+------------+--------------------+
| id   | name | work_date  |     no_of_posts    |
+------+------+------------+--------------------+
|    1 | Jim  | 2013-01-24 |                 50 |
|    2 | Rambo| 2013-05-27 |                 20 |
|    3 | Jack | 2013-05-06 |                 70 |
|    4 | Bill | 2013-04-06 |                 20 |
+------+------+------------+--------------------+
4 rows in set (0.02 sec)

Using IN clause it looks like :

mysql> SELECT * FROM user_posts_table 
    -> WHERE no_of_posts IN ( 50, 20, 70 );
+------+------+------------+--------------------+
| id   | name | work_date  |    no_of_posts     |
+------+------+------------+--------------------+
|    1 | Jim  | 2013-01-24 |                 50 |
|    2 | Rambo| 2013-05-27 |                 20 |
|    3 | Jack | 2013-05-06 |                 70 |
|    4 | Bill | 2013-04-06 |                 20 |
+------+------+------------+--------------------+
4 rows in set (0.02 sec)
mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform. Build dashboards, charts & reports for your business in minutes. Try it for free!