Lesson

MySQL OR Operator

10 min reading
Free Course

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/OR Logic: Because AND has higher precedence than OR, WHERE a = 1 AND b = 2 OR c = 3 means (a = 1 AND b = 2) OR c = 3. Add parentheses explicitly so your intent is unmistakable.
  • Replace Same-Column OR Chains with IN: Instead of writing status = 'paid' OR status = 'shipped' OR status = 'delivered', write status IN ('paid', 'shipped', 'delivered') for cleaner SQL and faster range optimization.
  • Watch Cross-Column OR Index Scans: Filtering WHERE customer_id = 10 OR email = 'a@b.com' can force a full table scan unless both columns are indexed individually or rewritten with UNION.

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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum