SQL
SQL OR Operator: Syntax, Usage, and Examples
The OR operator combines conditions and returns rows when at least one condition is true. Use it when you want broader matching instead of strict filtering.
How to Use the OR operator
You use the OR operator inside WHERE, HAVING, or join conditions. If any condition connected by OR evaluates to true, the row can appear in the result.
Learn SQL on Mimo
Basic syntax
SQL
SELECT column1, column2
FROM table_name
WHERE condition1OR condition2;
Syntax with multiple conditions
SQL
SELECT column1, column2
FROM table_name
WHERE condition1
OR condition2
OR condition3;
A quick way to think about it: OR is like a “pass if you match at least one rule” filter.
When to Use the OR operator
The OR operator shines when multiple different values or scenarios should be accepted. Here are some common, real-life use cases.
1) Matching multiple categories or values
Sometimes a user can fall into different acceptable groups. For example, a store might want to show products from several categories in one query.
Instead of running separate queries and stitching results together, OR handles it in one go.
2) Searching by different fields
Search features often accept multiple inputs. Someone might type an email, a username, or a full name.
Using OR lets your query match across multiple columns, so the search feels forgiving and user-friendly.
3) Handling “either this or that” business rules
Many rules allow more than one valid path:
- a person qualifies if they are a student or a senior
- an order counts if it’s paid or refunded (for reporting)
- an item ships if it’s in stock or it’s a preorder
That’s exactly what OR expresses.
4) Creating safety nets for missing data
If a field might be empty, you can use OR to keep useful results.
Example: show profiles that have a display name or a username, so you don’t end up with blank labels in a UI.
Examples of the OR operator
Here are practical examples that show how OR works in everyday SQL queries.
Example 1: Match multiple countries
You want customers from either Spain or Portugal.
SQL
SELECT customer_id, full_name, country
FROM customers
WHERE country='Spain'OR country='Portugal';
This keeps rows that match either condition.
Example 2: Match multiple statuses
You want to see orders that are either “Shipped” or “Delivered”.
SQL
SELECT order_id, status, shipped_date
FROM orders
WHERE status='Shipped'OR status='Delivered';
This is common in dashboards where you track “in progress or finished” states.
Example 3: Search by username or email
You want to find a user with a single search term, but you don’t know what field it belongs to.
SQL
SELECT user_id, username, email
FROM users
WHERE username='lina.dev'OR email='lina.dev@example.com';
If either value matches, the row appears.
Example 4: Filter by price range options
You want products that are cheap or premium, but you want to skip the middle tier.
SQL
SELECT product_id, product_name, price
FROM products
WHERE price<20OR price>200;
That query reads like a clear rule: “less than 20 or greater than 200.”
Example 5: Combine OR with AND (common trap)
You want users who are active and either admins or moderators.
✅ Correct:
SQL
SELECT user_id, role, is_active
FROM users
WHERE is_active=1
AND (role='Admin'OR role='Moderator');
Without parentheses, you might get inactive admins included by accident. That leads to messy reports and sometimes real security mistakes.
Learn More About the OR operator
OR looks simple, but it has a few sharp edges. Once you know them, you’ll avoid a lot of “why is my query returning this?” moments.
OR vs AND
These two operators do the opposite jobs.
ORreturns rows when at least one condition is trueANDreturns rows only when all conditions are true
Compare these two queries:
SQL
-- Broad results
SELECT*
FROM orders
WHERE status='Active'OR payment_status='Paid';
This can include:
- Active but unpaid
- Paid but not active
- Active and paid
Now compare it with AND:
SQL
-- Narrow results
SELECT*
FROM orders
WHERE status='Active'AND payment_status='Paid';
This returns only orders that match both conditions, so the list is smaller and stricter.
Why parentheses matter with OR
SQL usually evaluates AND before OR. So this:
SQL
WHERE condition1OR condition2AND condition3
is interpreted like:
SQL
WHERE condition1OR (condition2AND condition3)
If you meant “(condition1 OR condition2) AND condition3”, you must write it that way.
Here’s a real example:
You want orders that are either paid or refunded, but only for 2026.
✅ Correct:
SQL
SELECT order_id, status, created_at
FROM orders
WHERE (status='Paid'OR status='Refunded')
AND created_at>='2026-01-01';
That gives you the 2026 subset.
OR vs IN
OR is clear, but it gets repetitive when you match the same column to many values.
This works:
SQL
SELECT*
FROM products
WHERE category='Books'
OR category='Games'
OR category='Music';
But this is cleaner:
SQL
SELECT*
FROM products
WHERE categoryIN ('Books','Games','Music');
So, use OR when conditions differ, and use IN when the column stays the same.
OR with NULL values
NULL can change how conditions behave, because comparisons with NULL don’t return true or false in the usual way.
Example:
SQL
SELECT*
FROM users
WHERE phone_number=''OR phone_numberISNULL;
This returns users who either have an empty phone number or no phone number saved at all.
If you skip the IS NULL part, you might miss rows you expected to show up.
OR inside HAVING
You can use OR in HAVING when filtering grouped results.
Example: show teams that either have at least 10 members or have an average rating above 4.7.
SQL
SELECT team_id,COUNT(*)AS member_count,AVG(rating)AS avg_rating
FROM team_members
GROUPBY team_id
HAVINGCOUNT(*)>=10ORAVG(rating)>4.7;
That gives you groups that meet either requirement.
OR in JOIN conditions
Sometimes you join based on multiple possible relationships. This can happen in messaging apps, for example.
Example: show messages where the user is either the sender or the recipient.
SQL
SELECT message_id, sender_id, recipient_id, text
FROM messages
WHERE sender_id=42OR recipient_id=42;
That’s a classic “involved in the conversation” query.
Common OR mistakes (and how to avoid them)
1) Missing parentheses with AND
The fix is simple: group your OR conditions.
If you see both OR and AND in one query, add parentheses by default.
2) Using OR when you meant AND
This happens when you’re tired and your brain goes “yeah, both things matter.”
Example:
SQL
WHERE country='France'OR country='Italy'
That returns both countries, which might be correct.
But this will never return anything:
SQL
WHERE country='France'AND country='Italy'
A row can’t be both at the same time, unless the column contains multiple values.
3) Broad results that look “wrong”
If your query returns way too many rows, check your OR logic first. OR expands results quickly, especially across different columns.
Summary
The OR operator returns rows when at least one condition matches, making it ideal for flexible filtering and search-style queries. You’ll use it to match multiple values, handle alternative business rules, and build “either-or” logic without extra queries. Keep parentheses in mind when mixing OR with AND, and reach for IN when you’re matching many values on the same column.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot