HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

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:

SELECT * FROM employee WHERE Country = 'India' OR Country= 'Spain' ;
You can click on above box to edit the code and run again.

Output

or operator_country

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:

demo employee table

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

or conditions

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":

SELECT * FROM employee
WHERE Country = 'India' AND (Employee_name LIKE 'A%' OR Employee_name LIKE 'R%' );

You can click on above box to edit the code and run again.

Output

and or combination
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:

Example

 SELECT  *  FROM  employee 
WHERE Country = 'India' AND Employee_name LIKE 'A%' OR Employee_name LIKE 'R%' ;

You can click on above box to edit the code and run again.

Output

and or without parenthesis combination