How IN Clause Works

Hi All,

This Article is to understand use of IN clause.

IN clause is like OR Clause, but in OR clause we have to use multiple conditions, but Here in “IN” clause give conditions in comma separated string.

Let’s try to understand following example, it will clear many things.

We have table

 

+----+-----------+-------+
| id | product   | color |
+----+-----------+-------+
|  1 | Shirt     | RED   |
|  2 | pants     | Blue  |
|  3 | T-shirt   | RED   |
|  4 | watch     | RED   |
|  5 | Bed Sheet | white |
|  6 | socks     | Blue  |
+----+-----------+-------+

 

Now we need those rows which have color ‘Red’ and ‘Blue’

If we use OR clause in query to get required result.

………………………………………………………………………………………………………………

SELECT * FROM table1 WHERE color=’red’ OR color=’blue’;

OUTPUT:-

+----+---------+-------+
| id | product | color |
+----+---------+-------+
|  1 | Shirt   | RED   |
|  2 | pants   | Blue  |
|  3 | T-shirt | RED   |
|  4 | watch   | RED   |
|  6 | socks   | Blue  |
+----+---------+-------+

……………………………………………………………………………………………………………….
Or if we use IN clause

………………………………………………………………………………………………………………

SELECT * FROM table1 WHERE color IN (‘Red’,’Blue’);

OUTPUT:-

+----+---------+-------+
| id | product | color |
+----+---------+-------+
|  1 | Shirt   | RED   |
|  2 | pants   | Blue  |
|  3 | T-shirt | RED   |
|  4 | watch   | RED   |
|  6 | socks   | Blue  |
+----+---------+-------+

……………………………………………………………………………………………………………….

1 Comment

Post a Comment