Course contentsShow
← Data Science
Lesson 867 of 2,14522. SQL FoundationsFree lesson

The OR Operator

Using OR to match rows when any condition is true, broadening result sets with alternative criteria.

The OR Operator

You'll learn how to use OR to retrieve rows that match any of multiple conditions, expanding your result set.

What OR Does

While the AND operator narrows your results by requiring all conditions to be true, the OR operator does the opposite—it broadens your results by retrieving rows where any of the conditions are true. Think of it like casting a wider net: you're saying "give me this or that or something else."

How OR Works

When you connect conditions with OR, a row is included in the results if it satisfies at least one of the conditions. If any single condition evaluates to true, the entire WHERE clause becomes true for that row.

SELECT customer_name, country
FROM customers
WHERE country = 'USA' OR country = 'Canada';

This query returns customers from the USA, Canada, or both—anyone who matches either criterion.

Combining Multiple Conditions

You can chain multiple OR conditions together:

SELECT product_name, category
FROM products
WHERE category = 'Electronics' OR category = 'Books' OR category = 'Toys';

This retrieves products from any of the three categories.

Analogy: The Flexible Filter

Imagine you're at a restaurant saying "I'll eat pizza OR pasta OR salad." You're happy with any of those options—you're not requiring all three! Similarly, OR gives SQL flexibility to match diverse criteria in a single query.

Key Takeaway: Use OR when you want to broaden your filter criteria, retrieving rows that match any of the specified conditions rather than all of them.