00:00

MySQL OR Condition

Last updated: 4 min read

The OR operator combines conditions so that a row is returned if at least one of them is true. Use it for alternatives: hotels in Goa or with a pool, orders that are either cancelled or refunded.

1. Syntax and Meaning

syntax.sql
WHERE condition1 OR condition2 [OR condition3 ...]
OR keeps rows that satisfy at least one conditionTwo overlapping circles: hotels in Goa and hotels with a pool. OR returns everything in either circle, including the overlap.city = 'Goa'pool = TRUEOR = everything in either circle
Figure 1: OR widens the result. Every extra OR condition can only add rows.

Table: hotels (7 rows)

hotel_idnamecityratingpricepoolspa
1Sea BreezeGoa4.5620010
2Palm CourtGoa3.8390000
3Hill ViewShimla4.2540001
4Lagoon BayGoa4.1880011
5City InnJaipur3.5250000
6Fort PalaceJaipur4.71200011
7Snow PeakShimlaNULL4700NULL0

2. Basic OR

query.sql
SELECT name, city, pool FROM hotels WHERE city = 'Goa' OR pool = TRUE;

Result: 4 rows

namecitypool
Sea BreezeGoa1
Palm CourtGoa0
Lagoon BayGoa1
Fort PalaceJaipur1

3. Several OR Conditions

query.sql
SELECT name, price, rating FROM hotels WHERE price < 3000 OR rating >= 4.5 OR spa = TRUE;

Result: 5 rows

namepricerating
Sea Breeze62004.5
Hill View54004.2
Lagoon Bay88004.1
City Inn25003.5
Fort Palace120004.7

4. OR Truth Table (with NULL)

ABA OR B
TRUEanything (even NULL)TRUE
FALSEFALSEFALSE
FALSENULLNULL
NULLNULLNULL
query.sql
SELECT (1=1) OR NULL AS true_or_null, (1=2) OR NULL AS false_or_null, (1=2) OR (2=3) AS false_or_false;

Result: 1 row

true_or_nullfalse_or_nullfalse_or_false
1NULL0

Snow Peak has NULL rating and NULL pool. It is returned only when one condition is definitely TRUE:

query.sql
SELECT name FROM hotels WHERE rating > 4 OR pool = TRUE;

Snow Peak not included (both NULL)

name
Sea Breeze
Hill View
Lagoon Bay
Fort Palace
query.sql
SELECT name FROM hotels WHERE rating > 4 OR city = 'Shimla';

Snow Peak included (city is TRUE)

name
Sea Breeze
Hill View
Lagoon Bay
Fort Palace
Snow Peak

5. OR on One Column: Use IN

query.sql
SELECT name, city FROM hotels WHERE city = 'Goa' OR city = 'Shimla' OR city = 'Ooty';

Result: 5 rows

namecity
Sea BreezeGoa
Palm CourtGoa
Hill ViewShimla
Lagoon BayGoa
Snow PeakShimla
query.sql
SELECT name, city FROM hotels WHERE city IN ('Goa', 'Shimla', 'Ooty');

Result: 5 rows

namecity
Sea BreezeGoa
Palm CourtGoa
Hill ViewShimla
Lagoon BayGoa
Snow PeakShimla

Same result; IN is shorter and easier to maintain.

6. Mixing OR with AND

AND binds tighter than OR. Without parentheses, this means city = 'Goa' OR (city = 'Jaipur' AND price < 5000):

query.sql
SELECT name, city, price FROM hotels WHERE city = 'Goa' OR city = 'Jaipur' AND price < 5000;

Result: 4 rows

namecityprice
Sea BreezeGoa6200
Palm CourtGoa3900
Lagoon BayGoa8800
City InnJaipur2500
query.sql
SELECT name, city, price FROM hotels WHERE (city = 'Goa' OR city = 'Jaipur') AND price < 5000;

Result: 2 rows

namecityprice
Palm CourtGoa3900
City InnJaipur2500

See MySQL AND with OR for more.

7. XOR: Exactly One of Two

MySQL also has XOR: true when exactly one side is true. Hotels with a pool or a spa, but not both:

query.sql
SELECT name, pool, spa FROM hotels WHERE pool XOR spa;

Result: 2 rows

namepoolspa
Sea Breeze10
Hill View01

8. OR in UPDATE

update-or.sql
UPDATE hotels SET price = ROUND(price * 0.9)
WHERE rating < 4 OR rating IS NULL;
SELECT name, rating, price FROM hotels WHERE rating < 4 OR rating IS NULL;

Result: 3 rows

nameratingprice
Palm Court3.83510
City Inn3.52250
Snow PeakNULL4230

9. OR and Indexes

An index on one column can't serve a = 1 OR b = 2 by itself. MySQL may combine two indexes (index merge union) or fall back to a full scan. For large tables, rewriting as UNION of two indexed queries is a classic fix:

union-rewrite.sql
SELECT name FROM hotels WHERE city = 'Goa'
UNION
SELECT name FROM hotels WHERE spa = TRUE;

Result: 5 rows

name
Sea Breeze
Palm Court
Lagoon Bay
Hill View
Fort Palace

10. Practice Exercises

Exercise 1: Hotels that are very cheap (under 3,000) or very expensive (over 10,000).
solution.sql
SELECT name, price FROM hotels WHERE price < 3000 OR price > 10000;

Result: 2 rows

nameprice
City Inn2250
Fort Palace12000
Exercise 2: Shimla or Jaipur hotels with a rating of at least 4.
solution.sql
SELECT name, city, rating FROM hotels WHERE (city = 'Shimla' OR city = 'Jaipur') AND rating >= 4;

Result: 2 rows

namecityrating
Hill ViewShimla4.2
Fort PalaceJaipur4.7

11. Summary

  • OR returns rows where at least one condition is TRUE.
  • TRUE OR NULL is TRUE; FALSE OR NULL is NULL.
  • Use IN for many ORs on one column; use parentheses when mixing OR with AND.
  • XOR means exactly one; OR across columns may need index merge or a UNION rewrite.