SQL Learning Hub
SQL OR Operator
SQL OR Operator
Learn how to combine multiple conditions in SQL queries using the OR operator where at least one condition must be true
Understanding the SQL OR Operator
The OR operator in SQL is used to combine multiple conditions in a WHERE clause or HAVING clause. For a row to be included in the results, at least one of the conditions joined by OR must evaluate to TRUE.
Basic Syntax
Common SQL OR Operator Interview Questions
- How does the OR operator work with three or more conditions?
- What is the order of operations when combining OR with AND?
- How does the OR operator handle NULL values?
- What is the performance impact of using OR conditions?
How the SQL OR Operator Works
Basic OR Usage
When you use the OR operator, at least one condition must be TRUE for a row to be included in the results. If all conditions are FALSE or NULL, the row is excluded.
Combining Multiple OR Conditions
You can chain multiple conditions with OR to create more inclusive filters. If any condition is TRUE, the row will be included.
Truth Table for OR
The OR operator follows this truth table for two conditions A and B:
A | B | A OR B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
TRUE | NULL | TRUE |
FALSE | NULL | NULL |
NULL | NULL | NULL |
Understanding SQL's Three-Valued Logic with NULLs
In SQL, NULL represents an unknown or missing value. The OR operator follows these rules when dealing with NULLs:
- TRUE OR NULL = TRUE - When one value is TRUE, the result is always TRUE regardless of NULL
- FALSE OR NULL = NULL - When one value is FALSE and another is NULL, the result is NULL (unknown)
- NULL OR NULL = NULL - When both values are NULL, the result is NULL
Important: In WHERE clauses, only rows where the condition evaluates to TRUE are included in the result set. Rows where the condition evaluates to FALSE or NULL are both excluded.
OR with Other Logical Operators
Order of Operations: OR and AND
When combining OR and AND operators, AND has higher precedence than OR. This means that AND conditions are evaluated first, then OR conditions.
Warning: This is a common source of logical errors in SQL. Always use parentheses to make your intention clear.
Combining OR with AND Using Parentheses
Use parentheses to explicitly control the order of evaluation when combining OR and AND.
Note: The first query finds both tutorials and guides, but only if they have over 1000 views. The second finds all tutorials regardless of views, plus guides with over 1000 views.
Combining OR with NOT
Use NOT with OR to create complex filtering conditions.
Tip: When using NOT with OR, remember that NOT (A OR B) is equivalent to (NOT A) AND (NOT B) - this is De Morgan's Law.
Practical SQL OR Examples
Example 1: Multi-Keyword Content Search
This example shows how to search for videos with multiple keyword patterns:
Optimization Tip: For searching across many values in the same column, consider using the IN operator instead of multiple OR conditions. Example: WHERE title IN ("tutorial", "guide", "how to")
Example 2: Flexible Content Discovery
This example shows how to find popular content using different criteria:
Example 3: Complex Filtering with OR and AND
This example demonstrates proper use of parentheses with OR and AND:
Translation: This query finds videos that are: (1) either about tutorials OR education, AND (2) either have few views (< 1000) OR are very popular (> 50000 views), AND (3) uploaded this year.
OR vs. IN Operator
When checking a column against multiple possible values, you can use either OR or the IN operator. The IN operator is generally more concise and often performs better.
Best Practices for SQL OR Operator
1. Use Parentheses for Clarity
Always use parentheses when combining OR with AND operators to make your query's logic clear and avoid unexpected results due to operator precedence.
2. Consider Using IN Instead of Multiple ORs
When checking the same column against multiple values, the IN operator is more concise and often performs better than multiple OR conditions.
3. Be Aware of Performance Implications
OR conditions can sometimes prevent the database from using indexes effectively. In performance-critical scenarios, consider restructuring the query or using UNION to combine separate queries.
4. Understand NULL Behavior with OR
Remember that OR with NULL follows three-valued logic. If one condition is TRUE, the result is TRUE regardless of the other. If one condition is NULL and the other is FALSE, the result is NULL.
This can lead to unexpected filtering behavior. When a WHERE clause filters with an OR condition:
- Rows are included when the entire condition is TRUE
- Both FALSE and NULL results are excluded from the result set
- TRUE OR NULL always evaluates to TRUE, so rows will be included
Test Your Knowledge
Take a quick quiz to reinforce what you've learned about SQL OR Operator
Ready for hands-on SQL practice?
We have 200+ questions on real companies and real products.