I understand that SQL uses three valued logic but I am having trouble understanding how to use this in practice, especially why TRUE || NULL = True
and FA
The code example by user4955163 is a great visualization of this, however I just wanted to step back in to address the first segment of the question:
...especially why TRUE || NULL = True and FALSE && NULL = False instead of evaluating to null...
TRUE || NULL = True
This is because the or
operator will short-circuit if one operand is already known to be true
. No matter what the second operand is (even if unknown, ie. "NULL"), it wouldn't make the expression false
since we already know the other operand is true
. or
only needs one operand to be true
, to evaluate to true
.
FALSE && NULL = False
This is because the and
operator will short-circuit if one operand is already known to be false
. No matter what the second operand is (even if unknown, ie. "NULL"), it wouldn't make the expression true
since we already know the other operand is false
. and
needs both operands to be true
to evaluate to true
.