The SQL OR operator returns a TRUE if either condition is TRUE. The following truth table shows all combinations of values for the condition (A OR B).
Notice that for the entire expression to be true, either A or B can be true. We can use this to our favor when writing queries. Consider the situation where you may need to match a column on one or more values.
This query returns People having a FirstName of either Gail or John:
SELECT BusinessEntityId, PersonType, FirstName, LastName FROM Person.Person WHERE FirstName = 'Gail' OR FirstName = 'John'
Things to consider when using SQL OR:
- When repeating OR clauses for the same field consider using the IN operator. It is shorter, and easier to read.
- Be careful mixing AND and OR operators together. Much like when you work with multiplication and addition, the operations follow a prescribed evaluation order.
- When in doubt, use parenthesis. Place them around the OR clauses to ensure the Boolean conditions are properly evaluated.
Additional SQL OR Resources
To learn more, check out these useful resources: