← Back to SQL, page 17

sql-0338 · Easy fresher

A query for shipped or pending orders of customer 2 also returns other customers' orders. What is wrong?

AND binds tighter than OR, so status = 'shipped' OR status = 'pending' AND customer_id = 2 means shipped orders of anyone, plus pending orders of customer 2. Add parentheses around the OR, or use status IN ('shipped', 'pending').

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

The query as written:

sql
SELECT id, customer_id, status FROM orders
WHERE status = 'shipped' OR status = 'pending' AND customer_id = 2
ORDER BY id;
idcustomer_idstatus
1011shipped
1021shipped
1043shipped
1052pending
106NULLshipped

The query returns every shipped order (101, 102, 104 and 106, which belong to other customers or none) plus order 105. The intended filter on customer 2 only applied to the pending branch.

A corrected version:

sql
SELECT id, customer_id, status FROM orders
WHERE status IN ('shipped', 'pending') AND customer_id = 2
ORDER BY id;
idcustomer_idstatus
1052pending

Now only order 105 qualifies, since Bilal's other order is cancelled. IN reads clearly and avoids the precedence trap; when OR is needed, wrap it in parentheses. The same precedence applies in every SQL engine.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

SELECT id, customer_id, status FROM orders
WHERE status = 'shipped' OR status = 'pending' AND customer_id = 2
ORDER BY id;

SELECT id, customer_id, status FROM orders
WHERE status IN ('shipped', 'pending') AND customer_id = 2
ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • AND is evaluated before OR
  • Parenthesise OR groups
  • IN lists avoid the trap

Common mistakes

  • Relying on left-to-right reading
  • Testing only with data where the bug is invisible

Follow-ups

  • How does NOT interact with AND and OR?
  • How would you write this with a customer list?

Tips

  • Rewrite the WHERE with parentheses