The SQL OR Operator
The WHERE clause can contain one or more OR operators.
The OR operator is used to filter records based on more than one condition,
like if you want to return all employee from India but also those from Spain:
Example
Select all employee from India or Spain:You can click on above box to edit the code and run again.
SELECT * FROM employee WHERE Country = 'India' OR Country= 'Spain' ;
Output
Syntax
SELECT column1, column2, ... FROM table_name
WHERE condition1 OR condition2 OR condition3...;
OR vs AND
The OR operator displays a record if any of the conditions are TRUE.
The AND operator displays a record if all the conditions are TRUE.
Demo Employee table
This employee table is used for examples:
At Least One Condition Must Be True
The following SQL statement selects all fields from employee where either City is "Ranchi", Employee_name starts with the letter "P" or Country is "Germany":
Example
Select all employee from India or Spain: SELECT * FROM employee WHERE City = 'Ranchi' OR Employee_name LIKE 'P%' OR Country= 'Germany' ;You can click on above box to edit the code and run again.
Output
Combining AND and OR
You can combine the AND and OR operators.
The following SQL statement selects all employee from India that starts with a "A" or an "R".
Make sure you use parenthesis to get the correct result.
Example
Select all Indian employee that starts with either "A" or "R":You can click on above box to edit the code and run again.
SELECT * FROM employee
WHERE Country = 'India' AND (Employee_name LIKE 'A%' OR Employee_name LIKE 'R%' );
Output
Without parenthesis, the select statement will return all employee from India that starts with a "A",
plus all customers that starts with an "R", regardless of the country value: