SQL OR Operator
📘 SQL OR Operator
The OR operator in SQL is used inside the WHERE clause to combine conditions.
A row is returned if at least one of the conditions is TRUE.
1. Basic Syntax
2. Example: Simple OR Condition
Get employees from either the IT or HR department:
If either condition matches, the row is returned.
3. Using OR with Different Columns
This returns:
-
Items priced under 50
-
OR items that are out of stock
4. Combining OR with AND
Logical grouping matters!
❌ Without parentheses (may cause unexpected results):
AND has higher precedence than OR.
✔ Correct (use parentheses):
5. OR with IN (Cleaner Syntax)
Instead of:
Use:
6. OR with LIKE
7. Real-World Example
Return users who are:
-
located in the US or
-
have verified their email
8. Common Mistake to Avoid
Don’t overuse OR when an index can’t help.
If conditions are very different, OR can slow down queries.
You can often rewrite OR as a UNION for better performance.
Example:
