Sub topic 1
Predicates in the WHERE clause
A predicate is a condition that SQL evaluates for each row. A WHERE clause uses predicates to keep only rows that match a condition. This chapter assumes you know basic SELECT statements, tables, rows, columns, comparison operators such as =, <, and >, and the AND and OR operators.
Prerequisites
You should be able to read a query such as:
SELECT id, name, salary
FROM employees;You should also understand that a table contains rows and that each row has values in named columns.
The working model
A query with a WHERE clause evaluates its predicate separately for every candidate row. Only rows for which the predicate is TRUE appear in the result.
SELECT id, name, department
FROM employees
WHERE department = 'Sales';For each employee, SQL compares the department value with 'Sales'. Employees in Sales pass the filter. Employees in other departments do not.
The WHERE clause filters rows before later operations such as grouping or ordering. A predicate can compare a column with a constant, another column, or the result of an expression.
SELECT id, name, salary
FROM employees
WHERE salary >= 60000;This keeps employees whose salary is at least 60,000. The comparison includes 60,000 because the operator is >=.
More than one condition
Use AND when every condition must be true. Use OR when at least one condition must be true.
SELECT id, name, department, salary
FROM employees
WHERE department = 'Sales'
AND salary >= 60000;This returns only Sales employees whose salary is at least 60,000.
SQL evaluates AND before OR. That rule can make an unparenthesized predicate mean something different from what you intended. Use parentheses to show the grouping explicitly.
SELECT id, name, department, salary
FROM employees
WHERE department = 'Sales'
AND (salary >= 60000 OR name LIKE 'A%');The query keeps Sales employees who either earn at least 60,000 or have a name beginning with A. Without parentheses, the OR would apply differently:
WHERE (department = 'Sales' AND salary >= 60000)
OR name LIKE 'A%';That version also returns employees named with A from every department.
NULL and predicate results
NULL means that a value is missing or unknown. It is not the same as zero or an empty string. A comparison involving NULL usually produces UNKNOWN, not TRUE or FALSE.
SELECT id, name
FROM employees
WHERE manager_id = 10;An employee whose manager_id is NULL does not match this predicate. SQL cannot conclude that an unknown manager ID equals 10, so the row is excluded.
To test for missing values, use IS NULL or IS NOT NULL.
SELECT id, name
FROM employees
WHERE manager_id IS NULL;Because WHERE keeps only TRUE, both FALSE and UNKNOWN rows are filtered out. This three-valued logic becomes especially important when you negate conditions with NOT.
> callout note > When a filter unexpectedly omits rows, check for NULL values and check whether AND, OR, and parentheses express the intended logic.
A systematic way to write predicates
Before writing a complex filter, describe the required rows in plain language. Then translate each requirement into a condition.
- Identify the column or expression to test.
- Choose the comparison or predicate operator.
- Combine conditions with
ANDorOR. - Add parentheses around each alternative group.
- Check how
NULLvalues should behave. - Test the query with rows that should pass and rows that should fail.
The predicates LIKE, IN, and BETWEEN provide shorter forms for common filtering tasks. They are still predicates evaluated by WHERE, so the same rules about NULL, logical operators, and parentheses apply.
Exercise
Which employees does this predicate return: department = 'Support' OR department = 'Sales' AND salary >= 50000?
Show answer
SQL evaluates AND first, so it returns every Support employee, plus Sales employees whose salary is at least 50,000. Use (department = 'Support' OR department = 'Sales') AND salary >= 50000 if both departments must meet the salary condition.