Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A developer writes WHERE status = 'PAID' OR status = 'SHIPPED' AND total > 100 and is surprised that small PAID orders appear. Explain the bug and fix it.

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.

SQL
CREATE TABLE orders (id int PRIMARY KEY, status text, total numeric(12,2));
INSERT INTO orders VALUES
(1, 'PAID', 250.00),
(2, 'PAID', 20.00), -- small PAID order
(3, 'SHIPPED', 300.00),
(4, 'SHIPPED', 15.00),
(5, 'NEW', 500.00);
SELECT id FROM orders
WHERE status = 'PAID' OR status = 'SHIPPED' AND total > 100
ORDER BY id;
-- 1
-- 2 <- the surprise: a 20.00 order
-- 3
How the database evaluates each row
Rowstatus = 'PAID'SHIPPED AND total > 100Result of OR
1 PAID 250TRUEFALSETRUE
2 PAID 20TRUEFALSETRUE (bug)
3 SHIPPED 300FALSETRUETRUE
4 SHIPPED 15FALSEFALSEFALSE
5 NEW 500FALSEFALSEFALSE
The AND is evaluated first, so the PAID test alone is enough to accept a row.

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.

SQL
-- Fix 1: group the OR explicitly
SELECT id FROM orders
WHERE (status = 'PAID' OR status = 'SHIPPED') AND total > 100
ORDER BY id;
-- 1
-- 3
-- Fix 2: no OR at all
SELECT id FROM orders
WHERE status IN ('PAID', 'SHIPPED') AND total > 100
ORDER BY id;
-- 1
-- 3

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.

Java
// String building: the bug is easy to create
String where = "status = 'PAID' OR status = 'SHIPPED'";
where += " AND total > 100"; // now reads PAID OR (SHIPPED AND total > 100)
// JPA Criteria builds a tree, so grouping follows the tree
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Order> o = q.from(Order.class);
Predicate statusOk = cb.or(cb.equal(o.get("status"), "PAID"),
cb.equal(o.get("status"), "SHIPPED"));
q.where(cb.and(statusOk, cb.gt(o.get("total"), 100))); // (PAID OR SHIPPED) AND total > 100

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.

Java
@Test
void excludesSmallPaidOrders() {
insertOrder(1, "PAID", "250.00");
insertOrder(2, "PAID", "20.00"); // the row that exposes the bug
insertOrder(3, "SHIPPED", "300.00");
insertOrder(4, "SHIPPED", "15.00");
List<Long> ids = repository.findLargeFulfilledOrderIds();
assertThat(ids).containsExactly(1L, 3L); // fails with the original WHERE clause
}

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.

What to say in the interviewAbout 70 seconds aloud

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.
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/