← Back to SQL, page 2

sql-0021 · Easy fresher

A query meant to find Sales or Support staff earning over 65000 returns a low-paid employee. What is wrong with WHERE dept_id = 2 OR dept_id = 3 AND salary > 65000?

AND binds more tightly than OR, so the condition is read as dept_id = 2 OR (dept_id = 3 AND salary > 65000). Every Sales employee passes regardless of salary. Add parentheses around the OR, or use dept_id IN (2, 3) AND salary > 65000.

Full answer

SQL evaluates NOT first, then AND, then OR, the same way multiplication comes before addition in arithmetic. The condition as written:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name, dept_id, salary
FROM employees
WHERE dept_id = 2 OR dept_id = 3 AND salary > 65000
ORDER BY name;
namedept_idsalary
Sara262000
Tom270000

The database read it as:

text
dept_id = 2  OR  (dept_id = 3 AND salary > 65000)

So both Sales employees pass on the first half alone, and Sara's 62000 sneaks through next to Tom. Support staff are checked against the salary rule, and both of them fail it. The output looks plausible, which is why this bug survives code review.

The fix is to say what you meant with parentheses:

sql
SELECT name, dept_id, salary
FROM employees
WHERE (dept_id = 2 OR dept_id = 3) AND salary > 65000
ORDER BY name;
namedept_idsalary
Tom270000

IN expresses the same thing with less room for mistakes:

sql
SELECT name, dept_id, salary
FROM employees
WHERE dept_id IN (2, 3) AND salary > 65000
ORDER BY name;
namedept_idsalary
Tom270000

A habit worth building: whenever a WHERE clause mixes AND and OR, add parentheses even if the default precedence happens to be right. The next person to edit the query will not have to work out the precedence rules in their head.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, dept_id, salary
FROM employees
WHERE dept_id = 2 OR dept_id = 3 AND salary > 65000
ORDER BY name;

SELECT name, dept_id, salary
FROM employees
WHERE (dept_id = 2 OR dept_id = 3) AND salary > 65000
ORDER BY name;

SELECT name, dept_id, salary
FROM employees
WHERE dept_id IN (2, 3) AND salary > 65000
ORDER BY name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Precedence is NOT, then AND, then OR
  • a OR b AND c means a OR (b AND c)
  • Parentheses or IN make the intent explicit
  • Always bracket mixed AND and OR

Common mistakes

  • Trusting that the output looks reasonable instead of checking the logic
  • Adding parentheses around the wrong pair of conditions

Follow-ups

  • How does NOT interact with AND and OR in the same expression?
  • Rewrite the condition using IN.

Tips

  • Write out how the database groups the condition before fixing it