OR operator in sql

OR operator in sql

Blog Bustle 15/03/2020

We use OR operator in sql with where clause to filter sql query result. We can join two conditions by OR operator in sql. We can use sql OR operator with select, update and delete sql query.

Note: Remember that when we combine two conditions by using OR operator in sql, then at least one condition should be true otherwise query will return no result.

Syntax

The basic syntax of OR operator in sql

1. OR operator in sql select query

select column_1, column_2, column_3, ....
from table_name
where condition_1 OR condition_2 OR condition_3 .... ;

2. OR operator in sql update query

update table_name
set column_1 = value_1,
column_2 = value_2,
column_3 = value_3,
....
where condition_1 OR condition_2 OR condition_3 .... ;

3. OR operator in sql delete query

delete column_1, column_2, column_3, ....
from table_name
where condition_1 OR condition_2 OR condition_3 .... ;

Let's look some examples of OR operator in sql.

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 all employees who belong to New york or Binghamton.

select * from employee where city = 'New york' or city = 'Binghamton';

id name city country
1 justin New york US
2 robin Binghamton US

If i want to find all employees id who belong to England country or Manchester city.

select id from employee where city = 'Manchester' or country = 'England';

id
3
4

Remember that first select database then run any sql query related to that database otherwise you will get 'no database selected' error.

So we can easily understand use of OR operator in sql by above examples. Hope you like this tutorial.

Other important links

Sql AND Operator

Sql LIKE Operator

Sql where clause

Sql rename table query

Insert into select query

OR operator in sql searches:

Tags: #mysql