Sql IN operator

Sql IN operator

Blog Bustle 19/03/2020

Sql IN operator is very useful when we have to apply multiple OR conditions for same column in sql query. Sql IN operator is a short way to write multiple OR conditions. Now we do not need to write multiple OR conditions for same column, we can use sql IN operator in place of multiple OR conditions. We can use Sql IN operator with select, delete and update query.

Sql IN operator syntax

1. Sql IN operator with select query

select column_1, column_2, column_3, ....
from table_name
where column_x IN(value_1,value_2,value_3,....);

2. Sql IN operator with update query

update table_name
set column_1 = value_1,
column_2 = value_2,
column_3 = value_3,
....
where column_x IN(value_1,value_2,value_3,....);

3. Sql IN operator with delete query

delete column_1, column_2, column_3, ....
from table_name
where column_x IN(value_1,value_2,value_3,....);

Let's look some examples to understand Sql IN operator.

I have employee table.

id name city country
1 justin New york US
2 robin Binghamton US
3 jessie Nottingham England
4 chris Manchester England

I want to find those employee records who belong to New york city and Nottingham city and sql query will be

select * from employee where city = 'New york' OR city = 'Nottingham';

or

select * from employee where city IN('New york','Nottingham');

id name city country
1 justin New york US
3 jessie Nottingham England

I want to find those employee records whose name is justin and chris and sql query will be

select * from employee where name IN('justin','chris');

id name city country
1 justin New york US
4 chris Manchester England

Always remember that first we select database before running any sql query related to that database otherwise we will get 'no database selected' error.

After looking these examples, we can easily use sql IN operator. Hope you like this tutorial.

Other important links

Sql NOT operator

Sql OR Operator

Sql AND Operator

Sql LIKE Operator

Sql IN operator searches:

Tags: #mysql