Sub topic 12
Combining Predicates and Operator Precedence
This chapter assumes you can write basic SELECT statements, use tables and columns, compare values with =, <, and >, and combine conditions with AND and OR. A predicate is a condition that evaluates whether a row matches a filter.
A WHERE clause can contain one predicate or several predicates. SQL combines them according to operator precedence, which is the order in which SQL evaluates operators. Understanding that order prevents a query from returning rows you did not intend to select.
The basic logic operators
AND requires both conditions to be true. OR requires at least one condition to be true.
SELECT id, name, department, salary
FROM employees
WHERE department = 'Sales'
AND salary >= 50000;This query returns employees who are in Sales and earn at least 50,000. An employee who satisfies only one condition is excluded.
With OR, either condition is sufficient:
SELECT id, name, department
FROM employees
WHERE department = 'Sales'
OR department = 'Support';This is equivalent to using IN:
SELECT id, name, department
FROM employees
WHERE department IN ('Sales', 'Support');The IN form is usually easier to read when you compare one column with several possible values.
Operator precedence
SQL normally evaluates predicates in this order:
| Priority | Operator | Meaning |
|---|---|---|
| 1 | `NOT` | Reverses one condition |
| 2 | `AND` | Requires both conditions |
| 3 | `OR` | Requires at least one condition |
Because AND has higher precedence than OR, this query:
SELECT id, name, department, salary
FROM employees
WHERE department = 'Sales'
OR department = 'Support'
AND salary >= 50000;is interpreted as:
SELECT id, name, department, salary
FROM employees
WHERE department = 'Sales'
OR (department = 'Support' AND salary >= 50000);Every Sales employee matches, regardless of salary. Support employees match only when their salary is at least 50,000.
If you want the salary condition to apply to both departments, use parentheses:
SELECT id, name, department, salary
FROM employees
WHERE (department = 'Sales' OR department = 'Support')
AND salary >= 50000;Now an employee must belong to either Sales or Support and must earn at least 50,000.
Grouping complex filters
Parentheses can create several logical groups. Consider a product search that accepts products in two departments, but only when they are active and either inexpensive or highly rated:
SELECT id, name, department, price, rating
FROM products
WHERE department IN ('Books', 'Games')
AND active = TRUE
AND (price < 20 OR rating >= 4.5);The database first evaluates the grouped price-or-rating condition. It then combines that result with the department and active conditions using AND.
The logical structure is:
`NOT` and unknown values
NOT reverses a predicate:
SELECT id, name
FROM employees
WHERE NOT (department = 'Finance' OR department = 'Legal');This excludes employees in either department.
SQL also has three-valued logic: a condition can be TRUE, FALSE, or UNKNOWN. Comparisons with NULL usually produce UNKNOWN, not TRUE or FALSE. The WHERE clause keeps only rows whose final result is TRUE.
For example, an employee with a NULL department does not satisfy either department = 'Finance' or department = 'Legal'. To handle that case explicitly, use IS NULL or IS NOT NULL.
SELECT id, name
FROM employees
WHERE department IS NULL
OR department = 'Unassigned';Exercise
Exercise
Rewrite this filter so that employees must be in Sales or Support, must be active, and must either earn at least 60,000 or have a name beginning with A: WHERE department = 'Sales' OR department = 'Support' AND active = TRUE AND salary >= 60000 OR name LIKE 'A%';
Show answer
WHERE (department IN ('Sales', 'Support')) AND active = TRUE AND (salary >= 60000 OR name LIKE 'A%');