PolarDB for PostgreSQL (Compatible with Oracle) supports three logical operators: AND, OR, and NOT. SQL uses a three-valued logic system where values can be true, false, or null (unknown). Understanding how these operators behave with null inputs helps you write correct filter conditions.
Operators
| Syntax | Returns | Behavior with null |
|---|
Boolean AND Boolean → Boolean | true only when both operands are true | Returns false if either operand is false; otherwise returns null |
Boolean OR Boolean → Boolean | true when either operand is true | Returns true if either operand is true; otherwise returns null |
NOT Boolean → Boolean | Negates the operand | Returns null if the operand is null |
Truth tables
AND and OR
| a | b | a AND b | a OR b |
|---|
| true | true | true | true |
| true | false | false | true |
| true | null | null | true |
| false | false | false | false |
| false | null | false | null |
| null | null | null | null |
NOT
| a | NOT a |
|---|
| true | false |
| false | true |
| null | null |
Examples
-- AND: both conditions must be true
SELECT true AND true; -- true
SELECT true AND false; -- false
SELECT true AND null; -- null
SELECT false AND null; -- false
-- OR: at least one condition must be true
SELECT true OR false; -- true
SELECT false OR null; -- null
SELECT true OR null; -- true
-- NOT: negates the value
SELECT NOT true; -- false
SELECT NOT false; -- true
SELECT NOT null; -- null
Usage notes
AND and OR are commutative: switching the left and right operands does not change the result. However, the database does not guarantee that the left operand is evaluated before the right operand. If your query depends on evaluation order — for example, to guard against a division-by-zero error — use a CASE expression instead.