3.2 Logical Conditions & Operator Precedence
Key Takeaways
- Oracle SQL evaluates compound conditions using Three-Valued Logic (3VL): TRUE, FALSE, and UNKNOWN.
- The AND operator requires both conditions to be TRUE, whereas OR requires only one condition to be TRUE.
- Oracle evaluates arithmetic first, then concatenation, then all SQL conditions (comparison operators, IS NULL, LIKE, BETWEEN, IN, EXISTS) at one equal precedence level, then NOT, then AND, then OR.
- Because AND has higher precedence than OR, unparenthesized compound conditions evaluate AND clauses first, frequently resulting in unexpected row inclusion.
- The condition x NOT IN (a, b, NULL) expands to (x != a AND x != b AND x != NULL), which evaluates to UNKNOWN for all rows where x is not in {a, b}, returning zero rows.
3.2 Logical Conditions & Operator Precedence
Quick Answer: Logical conditions combine multiple search criteria using
AND,OR, andNOT. Oracle uses Three-Valued Logic (3VL), where expressions evaluate toTRUE,FALSE, orUNKNOWN(when evaluatingNULL). In operator precedence,NOTbinds tighter thanAND, andANDbinds tighter thanOR. To prevent unexpected filtering behavior, always use parentheses to explicitly define compound boolean grouping.
The Three Logical Operators
When filtering rows, a query often needs to satisfy multiple criteria simultaneously or provide alternative criteria:
AND: ReturnsTRUEif both component conditions evaluate toTRUE.OR: ReturnsTRUEif either component condition evaluates toTRUE.NOT: Reverses the truth value of a condition (TRUEbecomesFALSE,FALSEbecomesTRUE, andUNKNOWNremainsUNKNOWN).
-- Both conditions must be satisfied
SELECT employee_id, last_name, department_id, salary
FROM employees
WHERE department_id = 80
AND salary > 10000;
-- Either condition can be satisfied
SELECT employee_id, last_name, department_id, job_id
FROM employees
WHERE department_id = 50
OR job_id = 'IT_PROG';
Three-Valued Logic (3VL) Truth Matrices
In relational databases, because NULL represents missing or unknown data, comparison with NULL yields UNKNOWN. Oracle evaluates boolean conditions using Three-Valued Logic.
The AND Truth Table
For AND to return TRUE, both operands must be TRUE. If either operand is FALSE, the result is FALSE regardless of the other operand.
| Operand 1 | Operator | Operand 2 | Result |
|---|---|---|---|
TRUE | AND | TRUE | TRUE |
TRUE | AND | FALSE | FALSE |
TRUE | AND | UNKNOWN | UNKNOWN |
FALSE | AND | TRUE | FALSE |
FALSE | AND | FALSE | FALSE |
FALSE | AND | UNKNOWN | FALSE |
UNKNOWN | AND | TRUE | UNKNOWN |
UNKNOWN | AND | FALSE | FALSE |
UNKNOWN | AND | UNKNOWN | UNKNOWN |
The OR Truth Table
For OR to return TRUE, at least one operand must be TRUE. If one operand is TRUE, the entire condition evaluates to TRUE even if the other operand is UNKNOWN.
| Operand 1 | Operator | Operand 2 | Result |
|---|---|---|---|
TRUE | OR | TRUE | TRUE |
TRUE | OR | FALSE | TRUE |
TRUE | OR | UNKNOWN | TRUE |
FALSE | OR | TRUE | TRUE |
FALSE | OR | FALSE | FALSE |
FALSE | OR | UNKNOWN | UNKNOWN |
UNKNOWN | OR | TRUE | TRUE |
UNKNOWN | OR | FALSE | UNKNOWN |
UNKNOWN | OR | UNKNOWN | UNKNOWN |
The NOT Truth Table
| Operand | Operator | Result |
|---|---|---|
TRUE | NOT | FALSE |
FALSE | NOT | TRUE |
UNKNOWN | NOT | UNKNOWN |
[!IMPORTANT] A row is returned by a
WHEREclause only if the final condition evaluates toTRUE. Rows evaluating toFALSEorUNKNOWNare excluded from the query result.
Oracle Operator Precedence Hierarchy
When an expression contains multiple operators without enclosing parentheses, Oracle applies default operator precedence rules. Operators with higher precedence are evaluated before operators with lower precedence.
| Precedence Level | Category | Operators |
|---|---|---|
| 1 (Highest) | Arithmetic Operators | *, /, +, - (and unary +, -) |
| 2 | Concatenation | || |
| 3 | SQL Conditions (all equal to one another) | =, !=, <>, ^=, <, >, <=, >=, IS [NOT] NULL, [NOT] LIKE, [NOT] BETWEEN ... AND ..., [NOT] IN, EXISTS |
| 4 | Logical Negation | NOT |
| 5 | Logical Conjunction | AND |
| 6 (Lowest) | Logical Disjunction | OR |
Exam Precision: Oracle documents every condition — comparison operators,
IS [NOT] NULL,LIKE,BETWEEN,IN, andEXISTS— at a single, equal precedence level. OnlyNOT,AND, andORsit below them, in that order. Do not memorize an invented ranking that putsBETWEENabove or belowIN; when two same-level conditions appear in one expression, use parentheses rather than relying on ordering.
Overriding Precedence with Parentheses
Because AND has higher precedence than OR, Oracle groups AND conditions first unless overridden with parentheses (...).
Example: Unparenthesized vs. Parenthesized Evaluation
Consider this query intended to find high-earning sales reps (salary > 10,000 in department 80) OR anyone in department 50:
SELECT last_name, department_id, salary
FROM employees
WHERE department_id = 50
OR department_id = 80
AND salary > 10000;
Step-by-Step Default Evaluation:
- Oracle encounters
ORandAND. - Because
ANDhas higher precedence thanOR, Oracle evaluatesdepartment_id = 80 AND salary > 10000first. - The condition is interpreted as:
(department_id = 50) OR ((department_id = 80) AND (salary > 10000)). - Result: Returns ALL employees in department 50 (regardless of their salary), plus employees in department 80 whose salary is over 10,000.
If the business requirement was to find employees in either department 50 or 80 who earn more than 10,000, parentheses are mandatory:
SELECT last_name, department_id, salary
FROM employees
WHERE (department_id = 50 OR department_id = 80)
AND salary > 10000;
With parentheses, Oracle evaluates the (department_id = 50 OR department_id = 80) group first, and only returns employees from those two departments who earn over 10,000.
The NOT IN with NULL Trap
A classic 1Z0-071 exam question tests what happens when NOT IN encounters a list containing a NULL value.
Understanding the Expansion
Recall how IN and NOT IN expand logically:
-
x IN (10, 20, NULL)expands to:(x = 10 OR x = 20 OR x = NULL)- If
x = 10,(TRUE OR FALSE OR UNKNOWN)->TRUE(Row returned!) - If
x = 30,(FALSE OR FALSE OR UNKNOWN)->UNKNOWN(Row excluded)
- If
-
x NOT IN (10, 20, NULL)expands to:(x != 10 AND x != 20 AND x != NULL)- If
x = 10,(FALSE AND TRUE AND UNKNOWN)->FALSE(Row excluded) - If
x = 30,(TRUE AND TRUE AND UNKNOWN)->UNKNOWN(Row excluded!)
- If
Because x != NULL always evaluates to UNKNOWN, the entire AND chain can never evaluate to TRUE for any value of x. Consequently, x NOT IN (..., NULL) always returns ZERO rows.
-- DANGER: Returns 0 rows if any employee has manager_id IS NULL
SELECT employee_id, last_name
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees);
-- SAFE: Explicitly filter out NULLs in the subquery
SELECT employee_id, last_name
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees WHERE manager_id IS NOT NULL);
Complex Boolean Expression Walkthrough
Let us trace how Oracle evaluates a multi-operator condition step-by-step:
WHERE NOT department_id = 50 AND salary > 5000 OR job_id = 'AD_PRES'
- Step 1 (Comparisons): Evaluate comparisons
department_id = 50,salary > 5000,job_id = 'AD_PRES'. - Step 2 (
NOT): EvaluateNOT (department_id = 50), equivalent todepartment_id <> 50. - Step 3 (
AND): Evaluate(department_id <> 50) AND (salary > 5000). - Step 4 (
OR): Evaluate((department_id <> 50 AND salary > 5000) OR (job_id = 'AD_PRES')).
Consider the following query executed against the EMPLOYEES table: SELECT employee_id, last_name, department_id, salary FROM employees WHERE department_id = 50 OR department_id = 80 AND salary > 10000; Which set of records does this query return?
Under Oracle's Three-Valued Logic (3VL), what is the evaluation result of the boolean expression: (TRUE OR UNKNOWN) AND (FALSE OR UNKNOWN)?
Why does the condition department_id NOT IN (10, 20, NULL) return zero rows for all records in a table, even for employees in department 50?