SQL – AND Versus OR Operators

Now that we know how to apply a WHERE clause filter and are able to use multiple column filters we can introduce the OR operator. It’s not specifically a SQL AND versus OR, but using both conditions as a complement to each other as they’ll both become very useful when writing queries as we’ll see in this section.

Let’s start with a SQL query with multiple filters using AND:

sql example showing filtering by company and state

As we see from the results, there’s one record in the database with a Customer. That customer works for Apple and is located in the state of California. The nice thing about SQL is that it’s very easy to convert to an English statement. We could read the above statement as – “Give me (SELECT) all columns (*) from the customer table where the company is Apple and it’s located in California”.

What happens if we replace AND with OR instead?

sql example of showing the OR operator

The results have changed. There are 3 records now instead of the one – but why? This is because the database returned any Apple customers OR customers from the state of California. When we use AND it further restricts the filtering. Using OR increases the possibilities based on the new filter provided. The OR operator can be thought of as a way to supplement data into a query.

Let’s add another OR operator just to get the point across:

sql example showing multiple or statements

We’ve now asked the database to additionally include all customers that live in the state of Illinois. We could continue to add AND conditions that would filter the data and we could include OR statements that would add additional results. As an alternative way to write a SQL query – the AND/OR operators could be used when including joins. We will discuss this use case in another section.

Next Section: Parentheses – Order of Operations