6.1. Logical Operators

Logical Operators

OperatorDescriptionExample
ANDTrue if both values are truea AND b
ORTrue if either value is truea OR b
NOTTrue if the value is falseNOT a

Effect of NULL on Logical Operators

The result of an AND comparison may be NULL if one or bothsides of the expression are NULL. If at least one side of anAND operator is FALSE the expression evaluates to FALSE:

  1. SELECT CAST(null AS boolean) AND true; -- null
  2.  
  3. SELECT CAST(null AS boolean) AND false; -- false
  4.  
  5. SELECT CAST(null AS boolean) AND CAST(null AS boolean); -- null

The result of an OR comparison may be NULL if one or bothsides of the expression are NULL. If at least one side of anOR operator is TRUE the expression evaluates to TRUE:

  1. SELECT CAST(null AS boolean) OR CAST(null AS boolean); -- null
  2.  
  3. SELECT CAST(null AS boolean) OR false; -- null
  4.  
  5. SELECT CAST(null AS boolean) OR true; -- true

The following truth table demonstrates the handling ofNULL in AND and OR:

aba AND ba OR b
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
TRUENULLNULLTRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE
FALSENULLFALSENULL
NULLTRUENULLTRUE
NULLFALSEFALSENULL
NULLNULLNULLNULL

The logical complement of NULL is NULL as shown in the following example:

  1. SELECT NOT CAST(null AS boolean); -- null

The following truth table demonstrates the handling of NULL in NOT:

aNOT a
TRUEFALSE
FALSETRUE
NULLNULL