Logical Operators

Logical Operators

Logical terms let you combine other expressions using Boolean logic. N1QL provides the following logical operators:

  • AND
  • OR
  • NOT

In N1QL, logical operators have their usual meaning. However, in any of these expressions, if any operand is MISSING, the value of the expression is MISSING. If any operand is NULL and no operand is MISSING, the value of the expression is NULL. See the description of the OR operator for an exception to this rule.

AND

condition AND condition

AND evaluates to TRUE only if both conditions are TRUE.

Table 1. AND Truth Table
  TRUE FALSE
TRUE TRUE FALSE
FALSE FALSE FALSE

OR

condition OR condition

OR evaluates to TRUE if one of the conditions is TRUE.

Table 2. OR Truth Table
  TRUE FALSE
TRUE TRUE TRUE
FALSE TRUE FALSE
Note: The OR operator has an exception to the general rule that operators return MISSING or NULL if their operands are MISSING or NULL. If one operand of the OR operator is TRUE and the other operand is MISSING or NULL, the OR operator returns TRUE.

NOT

NOT condition

NOT evaluates to TRUE if the expression does not match the condition.

Logical Truth Table

x y x AND y x OR y NOT x
TRUE TRUE TRUE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
TRUE NULL NULL TRUE FALSE
TRUE MISSING MISSING TRUE FALSE
     
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE
FALSE NULL FALSE NULL TRUE
FALSE MISSING MISSING MISSING TRUE
     
NULL TRUE NULL TRUE NULL
NULL FALSE FALSE NULL NULL
NULL NULL NULL NULL NULL
NULL MISSING MISSING MISSING NULL
     
MISSING TRUE MISSING TRUE MISSING
MISSING FALSE MISSING MISSING MISSING
MISSING NULL MISSING MISSING MISSING
MISSING MISSING MISSING MISSING MISSING