Quick revision
The short answer
AND binds more tightly than OR, so the database reads the condition as status = 'PAID' OR (status = 'SHIPPED' AND total > 100). Every PAID order passes whatever its total, and only SHIPPED orders are checked against 100. The fix is to group the OR with parentheses, (status = 'PAID' OR status = 'SHIPPED') AND total > 100, or, more simply, to write status IN ('PAID', 'SHIPPED') AND total > 100.
What to remember
In this answer, we saw that AND binds more tightly than OR, so the original condition accepted every PAID order and checked the total only for SHIPPED ones. Parentheses or, better, status IN ('PAID', 'SHIPPED') fix it. The same bug appears in string-built SQL and in carelessly composed Criteria predicates, and a test with a small PAID order is the row that tells the two readings apart.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
AND binds more tightly than OR, so the database reads the condition as status = 'PAID' OR (status = 'SHIPPED' AND total > 100). Every PAID order passes whatever its total, and only SHIPPED orders are checked against 100. The fix is to group the OR with parentheses, (status = 'PAID' OR status = 'SHIPPED') AND total > 100, or, more simply, to write status IN ('PAID', 'SHIPPED') AND total > 100.
Why this question is asked
Operator precedence bugs are some of the most common logic errors in SQL, and they are hard to notice because the query runs without any error and returns plausible rows. They also show up in generated SQL, where a framework combines predicates for us. Interviewers ask this to see whether we can read a condition the way the database reads it, and whether we know how to protect ourselves with tests. In this answer, we will recap how precedence works, reproduce the bug on a small table, fix it two ways, look at how generated SQL causes the same problem, and see which test row would have caught it.
1A Quick Recap of Operator Precedence
Precedence is the rule that decides which operator is applied first when an expression has several of them. We know it from arithmetic: 2 + 3 * 4 is 14, not 20, because multiplication binds more tightly than addition. Boolean operators in SQL have the same kind of rule. NOT is applied first, then AND, then OR. In fact AND behaves like multiplication and OR like addition, which is an easy way to remember it.
So when SQL sees a OR b AND c, it reads a OR (b AND c). The developer in our question wanted (a OR b) AND c. The two readings agree for many rows, which is exactly why the bug slips through review.
2Reproducing the Bug
Let us build a tiny table with one row of each interesting kind and run the original condition.
| Row | status = 'PAID' | SHIPPED AND total > 100 | Result of OR |
|---|---|---|---|
| 1 PAID 250 | TRUE | FALSE | TRUE |
| 2 PAID 20 | TRUE | FALSE | TRUE (bug) |
| 3 SHIPPED 300 | FALSE | TRUE | TRUE |
| 4 SHIPPED 15 | FALSE | FALSE | FALSE |
| 5 NEW 500 | FALSE | FALSE | FALSE |
Row 2 is the small PAID order the developer did not expect. Row 4, the small SHIPPED order, is correctly excluded. That asymmetry is the tell-tale sign of a precedence bug: the total check is applied to one status but not to the other.
3Fixing It With Parentheses or IN
The direct fix is to add parentheses so the OR is evaluated first, which is what the developer meant. The cleaner fix is to replace the two equality tests with IN, which removes the OR altogether.
Both return only rows 1 and 3. I prefer the IN form because there is nothing to get wrong: IN is a single test, and its meaning does not depend on precedence. As a general habit, whenever a condition mixes AND and OR, I add parentheses even where they are not strictly needed, because the next reader should not have to remember the precedence table.
4How Generated SQL Produces the Same Bug
We rarely write every WHERE clause by hand. Search screens build conditions from optional filters, and frameworks combine predicates for us. If code concatenates strings, the bug appears easily: one part of the code produces status = 'PAID' OR status = 'SHIPPED', another part appends AND total > 100, and nobody adds the parentheses.
The Criteria API and Spring Data Specifications build a tree of predicates, and the provider adds parentheses when it renders that tree to SQL, so the grouping follows how we composed the objects. But the same mistake can still happen at the tree level if someone writes cb.or(paid, cb.and(shipped, big)). So whichever tool we use, it pays to log the generated SQL in tests and read it once.
5Writing a Test That Tells the Two Readings Apart
A test only protects us if it contains data on which the wrong condition and the right condition give different answers. Many test data sets have only large orders, and then both readings return the same rows. The row that distinguishes them is a small PAID order, because only the buggy reading accepts it.
This is a useful habit beyond this bug: for every condition, think of the row that sits on the boundary between the reading we want and the one we fear, and put it in the test.
6Follow-up questions
Does the same precedence apply in JPQL?
Yes. JPQL, the query language of Jakarta Persistence, follows the same order: NOT before AND before OR. So the JPQL query SELECT o FROM Order o WHERE o.status = 'PAID' OR o.status = 'SHIPPED' AND o.total > 100 has the same bug, and Hibernate translates it into SQL with the same meaning. The fixes are the same too: parentheses, or o.status IN ('PAID', 'SHIPPED').
Where does NOT fit in the order?
NOT binds most tightly of the three. So NOT a AND b means (NOT a) AND b, not NOT (a AND b). Comparison operators such as = and > bind more tightly still, which is why status = 'PAID' AND total > 100 works without parentheses around each comparison.
Could the database optimizer change the meaning by reordering conditions?
No. The optimizer may evaluate parts of a condition in any order it finds cheaper, but it must preserve the logical meaning of the expression as written. The bug is in what we wrote, not in how the database executes it.
7Conclusion
In this answer, we saw that AND binds more tightly than OR, so the original condition accepted every PAID order and checked the total only for SHIPPED ones. Parentheses or, better, status IN ('PAID', 'SHIPPED') fix it. The same bug appears in string-built SQL and in carelessly composed Criteria predicates, and a test with a small PAID order is the row that tells the two readings apart.
Open with
The bug is operator precedence. In SQL, AND binds more tightly than OR, just like multiplication over addition, so the condition is read as status equals PAID, OR status equals SHIPPED and total greater than 100.
Then, if they want more
That means every PAID order passes regardless of its total, while SHIPPED orders are checked against 100, which is why small PAID orders appear but small SHIPPED ones do not. I would fix it with parentheses around the OR, or better with status IN PAID, SHIPPED AND total greater than 100, which has no OR to get wrong.
I would also look at where the SQL came from, because this often happens when code concatenates predicates as strings or when a dynamic filter appends an AND to an existing OR. Criteria API and Specifications build a predicate tree and render the grouping for us, but only if we compose the tree correctly.
Finally, I would add a test with a small PAID order, because that is the row on which the buggy and the correct reading disagree. The same precedence applies in JPQL.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A developer writes WHERE status = 'PAID' OR status = 'SHIPPED' AND total > 100 and is...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/9e3a4bb4b3/