AND/OR Precedence and Parenthesization
The single most common filtering bug interviewers plant in their questions.
The Trap Interviewers Plant
The single most common filtering bug in interviews looks correct at a glance. You are asked: find orders from customer 5 that are either pending or shipped. A candidate writes a WHERE with AND and OR mixed together and gets extra rows back.
Why? Because AND binds tighter than OR. The database evaluates all the AND pairs first, then the ORs. This lesson makes that rule reflexive so you never lose points on it.
Operator Precedence in One Sentence
In every SQL dialect, the logical operators bind in this order: NOT first, then AND, then OR last.
NOTis highest precedenceANDis middleORis lowest
So A OR B AND C is read as A OR (B AND C), never (A OR B) AND C. The interviewer is counting on you forgetting this.
All lessons in this course
- AND/OR Precedence and Parenthesization
- BETWEEN, IN, and Inclusive Boundaries
- LIKE, Wildcards and Escaping
- Filtering on Calculated Values