OR Operator

So far, we have only been able to evaluate column based on a single filter logic. For example, select all records where the Country = 'Canada' or CustomerID > 10. However, suppose we want to have multiple logical filtering on a single column. The OR operator provides this functionality.

The OR operator filters the output of the query to return either of the logical comparisons. To demonstrate the use of the OR operator, we modify our query from the previous section to filter for either Canada or Germany.

select customer_id , ship_country , ship_via, freight
from orders
where (ship_country = 'Canada' or ship_country = 'Germany' )
     and freight >= 20
| customer_id | ship_country | ship_via | freight |
| ----------- | ------------ | -------- | ------- |
| 2001        | Canada       | 1        | 25.5    |
| 2005        | Canada       | 2        | 22.8    |
| 2007        | Germany      | 3        | 28.9    |
| 2010        | Canada       | 3        | 41.2    |
| 2015        | Germany      | 2        | 33.4    |
| 2018        | Canada       | 2        | 37.25   |
| 2019        | Germany      | 3        | 24.7    |

The output of the query will contain records that have either Canada and Germany value on the Country column. The OR operator supports as many filters as needed or can be added. We simply separate the filters by the OR operator.

Below, we add an additional filter to return all data that may contain Canada, Germany, and France.

select customer_id , ship_country , ship_via, freight
from orders
where (ship_country = 'Canada' or ship_country = 'Germany' OR ship_country = 'France' )
      and freight >= 20

| customer_id | ship_country | ship_via | freight |
| ----------- | ------------ | -------- | ------- |
| 2001        | Canada       | 1        | 25.5    |
| 2003        | France       | 1        | 32.25   |
| 2005        | Canada       | 2        | 22.8    |
| 2007        | Germany      | 3        | 28.9    |
| 2010        | Canada       | 3        | 41.2    |
| 2012        | France       | 2        | 26.85   |
| 2015        | Germany      | 2        | 33.4    |
| 2016        | France       | 3        | 29.95   |
| 2018        | Canada       | 2        | 37.25   |
| 2019        | Germany      | 3        | 24.7    |
| 2020        | France       | 1        | 42.15   |

As you can see, records with France on the Country column are not part of the results.

The OR operator is a simple and useful tool to evaluate a column against multiple conditions. In the next section, we look at the BETWEEN operator which evaluates filters within a specific range of values.