Part 15: Logical Operators: AND, OR & NOT (Precedence Rules)
Combining multiple conditions requires understanding operator precedence.
1. Operator Precedence Trap
Critical Rule: In SQL,
ANDtakes precedence overOR(just like multiplication takes precedence over addition in arithmetic).
-- What you might think this does: Employees in (HR or Sales) earning > 70000
-- What SQL actually executes: HR employees OR (Sales employees earning > 70000)
SELECT * FROM employees
WHERE department = 'HR' OR department = 'Sales' AND salary > 70000;
-- CORRECT: Always use explicit parentheses!
SELECT * FROM employees
WHERE (department = 'HR' OR department = 'Sales') AND salary > 70000;
Key Takeaways
ANDis evaluated beforeOR.- Always wrap conditions in explicit parentheses
()to avoid subtle logical bugs.
In Part 16, we explore Range Filtering with BETWEEN and NOT BETWEEN!

