Module 02 · lesson 8
Parentheses and condition precedence
The task: "electronics or clothing, but only cheaper than $100". Without parentheses the query unexpectedly returns the pricey "Galaxy" and "ProBook". Why?
Precedence: AND is stronger than OR
When a condition has both AND and OR, the DBMS evaluates AND first and only then OR (AND has higher precedence). This is often not what we mean:
-- NOT what you expect:
WHERE category = 'Home' OR category = 'Books' AND price < 50
reads as Home OR (Books AND price < 50) — that is, all of Home passes regardless of price (even Espresso at $129).
Parentheses set the order
Parentheses make your intent explicit:
WHERE (category = 'Home' OR category = 'Books') AND price < 50
Now the price condition applies to both categories.
Rule: mixing AND and OR — add parentheses, even if it seems obvious as is.
Your turn
You need products from "Electronics" or "Clothing", and also cheaper than $100. Output name, category, price. Don't forget the parentheses!
Summary
ANDis evaluated beforeOR.- Parentheses make your intent explicit and protect against mistakes.