MySQL IN Condition

The MySQL IN condition is used to reduce the use of multiple OR conditions in a SELECT, INSERT, UPDATE and DELETE statement.

Syntax:

snippet
expression IN (value1, value2, .... value_n);

Parameters

expression: It specifies a value to test.

value1, value2, ... or value_n: These are the values to test against expression. If any of these values matches expression, then the IN condition will evaluate to true. This is a quick method to test if any one of the values matches expression.

MySQL IN Example

Consider a table "officers", having the following data.

MySQL IN Condition 1

Execute the following query:

snippet
SELECT *
FROM officers
WHERE officer_name IN ('Ajeet', 'Vimal', 'Deepika');

Output:

MySQL IN Condition 2

Let's see why it is preferred over OR condition:

Execute the following query:

snippet
SELECT *
FROM officers
WHERE officer_name = 'Ajeet'
OR officer_name = 'Vimal'
OR officer_name = 'Deepika';

Output:

MySQL IN Condition 3

It also produces the same result. So IN condition is preferred over OR condition because it has minimum number of codes.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents