How to combine conditions with AND OR NOT
· Category: SQL & Databases
Short answer
Use AND to require multiple conditions, OR to accept any condition, and NOT to negate a condition. Parentheses control evaluation order.
Steps
- Both conditions:
SELECT * FROM products WHERE price > 100 AND stock > 0; - Either condition:
SELECT * FROM products WHERE category = 'Electronics' OR category = 'Books'; - Negation:
SELECT * FROM products WHERE NOT discontinued; - Complex logic:
SELECT * FROM orders WHERE (status = 'shipped' OR status = 'delivered') AND order_date > '2025-01-01'; - Use
INas a cleaner alternative to multipleORequalities.
Tips
ANDhas higher precedence thanOR; use parentheses to make intent explicit.NOT INwith a subquery that returnsNULLyields no rows, which is a common trap.
Common issues
- Missing parentheses around
ORconditions combined withANDcauses logic errors. NOTwithNULLevaluates toNULL, not true.r