Combining WHERE with OR
beginnerfiltering
2 min read
What is OR in a WHERE Clause?
The OR operator combines conditions where at least one must be true. A row appears in the result if any of the OR conditions evaluates to true.
Syntax
SELECT * FROM table_name
WHERE condition1 OR condition2;When to Use
- Matching rows that satisfy any one of several criteria
- Searching for multiple values in a column (consider IN as an alternative)
- Building flexible search filters
Key Points
- Any Must Be True — At least one condition must be true for the row to be included.
- Precedence — AND is evaluated before OR. Use parentheses to clarify intent.
- IN Alternative —
WHERE col = 'a' OR col = 'b'can be written asWHERE col IN ('a', 'b'). - Performance — Multiple OR conditions on different columns may prevent index usage. Consider restructuring with UNION.
- Readability — Parenthesize OR groups when mixing with AND.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
Combining WHERE with OR Challenge
Write a query that find students with grade A or grade B.
Expected result
Students who have either grade A or grade B.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is OR in a WHERE Clause?
Initializing database...Each run starts from fresh sample data.
More Examples
OR combined with AND
Find students in grade A over 20, or in grade B.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Should I use OR or IN?
When comparing one column against multiple values, IN is cleaner: WHERE col IN ('a','b','c') instead of col = 'a' OR col = 'b' OR col = 'c'.
Why do I need parentheses with OR?
AND has higher precedence than OR. Without parentheses, A AND B OR C is interpreted as (A AND B) OR C, which may not be what you intend.