Almost every useful SELECT in DB2 for z/OS asks a question about rows: which employees, which orders, which accounts? Those questions are written as predicates—conditions that evaluate to TRUE, FALSE, or UNKNOWN. This overview introduces what predicates are, the main kinds Db2 supports, and how they fit into WHERE and related clauses.
A predicate is a SQL condition that tests data. Combined with AND, OR, and NOT, predicates form a search condition. The WHERE clause keeps rows for which that search condition is TRUE. HAVING does the same for groups after aggregation. JOIN ON clauses use predicates to decide which row combinations match.
12345SELECT EMPNO, LASTNAME, SALARY FROM HR.EMPLOYEE WHERE WORKDEPT = 'A00' AND SALARY > 50000 AND COMM IS NOT NULL;
Each of WORKDEPT = 'A00', SALARY > 50000, and COMM IS NOT NULL is a predicate. AND combines them into one search condition. Because of three-valued logic, a null SALARY makes SALARY > 50000 UNKNOWN, and that row drops out unless you handle nulls explicitly.
Predicates are not the same as expressions. An expression computes a value (SALARY * 1.03). A predicate asks a true/false/unknown question about values. You often put expressions inside predicates: SALARY * 1.03 > 60000.
Beginners usually meet predicates first in WHERE. Everything you learn there transfers to the other contexts with only small differences in when they run in the query pipeline.
| Kind | Example | Meaning |
|---|---|---|
| Basic | SALARY >= 50000 | Compare two expressions (=, <>, <, >, <=, >=) |
| BETWEEN | SALARY BETWEEN 40000 AND 60000 | Inclusive range test |
| IN | WORKDEPT IN ('A00','B01') | Match any value in a list or subquery |
| LIKE | LASTNAME LIKE 'S%' | Pattern match with % and _ |
| NULL | COMM IS NULL | Test absence of a value |
| EXISTS | EXISTS (SELECT 1 FROM …) | True if subquery returns a row |
A basic predicate compares two expressions (or row-value expressions) with =, <>, <, >, <=, or >=. Older not-equal spellings like != or ¬= exist for compatibility; prefer <> in new SQL. If either side is null (or a scalar subquery is empty where rules say so), the result is UNKNOWN.
123WHERE EMPNO = '528671' WHERE SALARY < 20000 WHERE SALARY >= (SELECT AVG(SALARY) FROM HR.EMPLOYEE)
BETWEEN tests an inclusive range. IN tests membership in a list or the result of a subquery. LIKE matches character patterns: % means any string, _ means any single character (with escape options when you need literal % or _).
123WHERE HIREDATE BETWEEN DATE '2020-01-01' AND DATE '2020-12-31' WHERE WORKDEPT IN ('A00', 'B01', 'C01') WHERE LASTNAME LIKE 'JO%N_';
Use IS NULL / IS NOT NULL to test nulls—never = NULL. EXISTS is true when a subquery returns at least one row (often more efficient than counting). IS DISTINCT FROM / IS NOT DISTINCT FROM compare with null-aware equality. Quantified predicates (= ANY, > ALL, and similar) compare a value to a set from a subquery. Specialized predicates such as XMLEXISTS and ARRAY_EXISTS appear when you work with XML or arrays.
AND, OR, and NOT combine predicates. Parentheses control grouping. Remember three-valued rules: FALSE AND anything is FALSE; TRUE OR anything is TRUE; NOT UNKNOWN is UNKNOWN.
123WHERE (WORKDEPT = 'A00' OR WORKDEPT = 'B01') AND NOT (SALARY IS NULL) AND (COMM > 1000 OR BONUS > 500);
Write the most selective, indexed predicates clearly. Avoid wrapping columns in functions when a plain comparison would do—YEAR(HIREDATE) = 2020 can prevent index use that HIREDATE BETWEEN … would allow. Performance tuning is a later topic; the habit of writing direct predicates starts here.
Db2’s optimizer reads your predicates when choosing an access path. Matching predicates on leading index columns, stage-1 versus stage-2 evaluation, and residual predicates all affect CPU and I/O. For this fundamentals page, remember: predicates are how you tell Db2 which rows matter, and clear predicates give the optimizer clearer choices.
A predicate is a yes/no question about each toy in a toy box: “Is it red?” “Is it bigger than this ball?” “Is the sticker missing?” Db2 walks the box and keeps only the toys where the answer is clearly yes. If the answer is “I can’t tell” (NULL / UNKNOWN), that toy does not stay in the keep pile. BETWEEN asks “is it between these two sizes?” LIKE asks “does the name look like this pattern?” EXISTS asks “is there at least one matching toy in another box?”
1. What is a predicate in Db2 SQL?
2. Which rows does WHERE keep?
3. Which is a basic predicate?
4. How do you test whether a column is null?
5. What does EXISTS check?