AND/OR Precedence and Parenthesization
The single most common filtering bug interviewers plant in their questions.
AND/OR Precedence and Parenthesization is a free SQL Interview Prep lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
The Buggy Query
Here is the query a candidate writes for customer 5, status pending or shipped. It looks right but is wrong.
Because AND binds tighter, SQL reads it as (customer_id = 5 AND status = 'pending') OR status = 'shipped'. The second branch has no customer filter at all, so it returns shipped orders from every customer.
SELECT *
FROM orders
WHERE customer_id = 5
AND status = 'pending'
OR status = 'shipped';The Fix: Parentheses
Wrap the OR branch in parentheses so the customer filter applies to both statuses. Parentheses override precedence and make intent explicit.
Now SQL evaluates the grouped OR first, then applies customer_id = 5 to the whole group. Senior reviewers add parentheses even when precedence already does the right thing, purely for readability.
SELECT *
FROM orders
WHERE customer_id = 5
AND (status = 'pending' OR status = 'shipped');Walking the Truth Table
Take a shipped order from customer 9. Trace the buggy version:
customer_id = 5is falsestatus = 'pending'is falsefalse AND falseis falsefalse OR (status = 'shipped' is true)is true
The row is returned even though it is not customer 5. Tracing one concrete row out loud is exactly what interviewers want to see.
NOT Binds Tightest
NOT applies only to the condition immediately after it. So NOT status = 'open' AND priority = 'high' means (NOT status = 'open') AND priority = 'high'.
If you meant to negate the whole thing, you must parenthesize: NOT (status = 'open' AND priority = 'high'). By De Morgan's law that equals status <> 'open' OR priority <> 'high'.
SELECT *
FROM tickets
WHERE NOT (status = 'open' AND priority = 'high');A Three-Condition Example
Find products that are (electronics or appliances) and in stock. Without parentheses the in-stock filter would only attach to appliances.
The grouped query below reads cleanly: pick one of two categories, then require stock. The mental rule: whenever a query mixes AND with OR, you almost always need parentheses around the OR group.
SELECT *
FROM products
WHERE (category = 'electronics' OR category = 'appliances')
AND in_stock = true;IN as a Cleaner Alternative
A long chain of ORs on the same column is both error-prone and verbose. Replace it with IN, which is unambiguous and needs no parentheses.
The query below is exactly equivalent to the previous one but harder to break. Interviewers like seeing this refactor because it shows you understand both correctness and readability.
SELECT *
FROM products
WHERE category IN ('electronics', 'appliances')
AND in_stock = true;Precedence in Computed Filters
Precedence also applies when arithmetic and logic mix. In price * 1.2 > 100 OR discount = 0 AND active = true, the comparison and arithmetic resolve first, then AND, then OR.
So it reads (price * 1.2 > 100) OR ((discount = 0) AND (active = true)). When in doubt, do not memorize the table under pressure, just parenthesize every group.
How to Answer It Live
When an interviewer shows you a mixed AND/OR filter and asks what does this return, do three things:
- State the precedence rule:
ANDbeforeOR - Re-parenthesize the query out loud to reveal its true grouping
- Trace one row that exposes the bug
Then offer the corrected, parenthesized version. That sequence reads as senior-level rigor.
Defensive Habit
The professional habit is: parenthesize every OR group, always. It costs two characters and removes an entire class of bugs and review comments.
Code reviewers should never have to mentally evaluate precedence to confirm a filter is correct. Make the grouping visible. This is one of the few cases where redundant syntax is considered good style by everyone.
SELECT order_id, status
FROM orders
WHERE customer_id = 5
AND (status = 'pending' OR status = 'shipped');Quick Check
Reason about the precedence rule before answering.
Recap
Key takeaways:
- Logical precedence is NOT, then AND, then OR
- Mixed
AND/ORfilters almost always need parentheses around theORgroup - A missing parenthesis silently widens results, often dropping a filter entirely
- Replace long
ORchains on one column withIN - To answer live: state the rule, re-parenthesize, trace one row, then fix
Always parenthesize OR groups, even when precedence already agrees.
Frequently asked questions
Is the “AND/OR Precedence and Parenthesization” lesson free?
Yes — the full text of “AND/OR Precedence and Parenthesization” is free to read here on the web, and the SQL Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “AND/OR Precedence and Parenthesization”?
The single most common filtering bug interviewers plant in their questions. You practise SQL Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SQL Interview Prep?
No prior experience is required. SQL Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “AND/OR Precedence and Parenthesization” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SQL Interview Prep lesson?
Yes. Every SQL Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- AND/OR Precedence and Parenthesization
- BETWEEN, IN, and Inclusive Boundaries
- LIKE, Wildcards and Escaping
- Filtering on Calculated Values