bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL OR Operator

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind SQL OR Operator?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ Customers
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Combining AND and OR
At Least One Condition Must Be True
The SQL OR Operator

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.

Note

The OR operator displays a record if any of the conditions are TRUE.

The following SQL selects all customers from Germany OR Spain:

Example

SELECT *

FROM Customers

WHERE Country = 'Germany' OR Country = 'Spain';

OR Syntax

SELECT column1 , column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ... ;

Demo Database

Below is a selection from the Customers table used in the examples:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

At Least One Condition Must Be True

The following SQL selects all customers where City is "Berlin", OR CustomerName starts with the letter "G", OR Country is "Norway":

Example

SELECT * FROM Customers

WHERE City = 'Berlin'
OR CustomerName LIKE 'G%'
OR Country = 'Norway';

AND vs. OR

The AND operator displays a record if all the conditions are TRUE.

The OR operator displays a record if any of the conditions are TRUE.

Combining AND and OR

You can also combine AND and OR operators.

The following SQL selects all customers from Spain that starts with a "G" or an "R" (make sure to use parenthesis to get the correct result):

Example

SELECT * FROM Customers

WHERE Country = 'Spain'
AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');

Without parenthesis, the SQL above will return all customers from Spain that starts with a "G", plus all customers that starts with an "R", regardless of the country value:

Example

SELECT * FROM Customers

WHERE Country = 'Spain'
AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';

Previous

SQL AND Operator

Next

SQL NOT Operator