MySQL OR Operator
MySQL OR Operator: Matching Any of Multiple Conditions
The MySQL OR operator combines two or more conditions in a WHERE or HAVING clause and returns a row if at least one condition is true. You use it whenever a query must match several alternative categories, statuses, or regions.
Basic Syntax
Place OR between alternative conditions inside WHERE:
SELECT columns
FROM table_name
WHERE condition1 OR condition2;
Practical SQL Example
This query fetches orders in demo_orders that are either still pending review or flagged for manual check.
SELECT id, customer_id, status, total_amount
FROM demo_orders
WHERE status = 'pending'
OR status = 'on_hold'
ORDER BY id DESC;
| id | customer_id | status | total_amount |
|---|---|---|---|
| 108 | 12 | on_hold | 149.00 |
| 104 | 7 | pending | 54.50 |
Common Mistakes and Best Practices
- Always Parenthesize Mixed
AND/ORLogic: BecauseANDhas higher precedence thanOR,WHERE a = 1 AND b = 2 OR c = 3means(a = 1 AND b = 2) OR c = 3. Add parentheses explicitly so your intent is unmistakable. - Replace Same-Column
ORChains withIN: Instead of writingstatus = 'paid' OR status = 'shipped' OR status = 'delivered', writestatus IN ('paid', 'shipped', 'delivered')for cleaner SQL and faster range optimization. - Watch Cross-Column
ORIndex Scans: FilteringWHERE customer_id = 10 OR email = 'a@b.com'can force a full table scan unless both columns are indexed individually or rewritten withUNION.
Frequently Asked Questions
When should I use IN instead of OR in MySQL?
Use IN (...) whenever every condition tests the exact same column against multiple constant values.
Why does OR across two unindexed columns slow down large tables?
MySQL cannot use a single B-tree index scan across two unrelated columns unless it merges separate index scans or falls back to a full table scan.
How does OR behave with NULL values?
TRUE OR UNKNOWN evaluates to TRUE, whereas FALSE OR UNKNOWN evaluates to UNKNOWN and filters the row out.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.