Logical operators allow you to test whether a condition is true or false. Therefore, the result returned by a logical operator is either true, false, or NULL (unknown).
You can think of a NULL result as an outcome that is undetermined. Therefore, it’s impossible to tell whether a comparison to NULL is true or false. Consider the following example:
(NULL = 1)
The comparison to NULL makes the first operand (i.e., the boolean expression you want to evaluate) unknown because it cannot be determined.
The focus of this mini-lesson is to review the logical operators you have seen in the previous video and how they operate.
AND
operatorThe AND
(or &&
) operator is a logical operator that
combines two boolean expressions.
The AND operator returns true if both expressions evaluate to true.
If one of the two expressions is false,
then the AND operator returns false, even if one of the expressions is NULL.
The syntax for the AND operator in SQL is given by:
expression1 AND expression2;
The table below summarizes the results when comparing boolean expressions using the AND operator.
True | False | NULL | |
---|---|---|---|
True | True | False | NULL |
False | False | False | False |
NULL | NULL | False | Null |
The OR
(or ||
) operator is a logical operator that
combines two boolean expressions.
The syntax for the OR operator in SQL is given by:
SELECT column1, column2,
FROM table1
WHERE expression1 OR expression2;
The table below summarizes the results when comparing boolean expressions using the OR
operator.
OR
operatorTrue | False | NULL | |
---|---|---|---|
True | True | True | True |
False | True | False | NULL |
NULL | True | NULL | NULL |
NOT
operatorThe NOT
(or !
) operator is a logical operator that
combines two boolean expressions.
It reverses the result of any logical operator.
The syntax for the OR operator in SQL is given by:
NOT [expression1];
The table below summarizes the results when comparing boolean expressions using the NOT operator.
NOT | |
---|---|
True | False |
False | True |
NULL | NULL |
XOR
OperatorThe XOR operator is a logical operator that combines two boolean expressions. It returns true if one expression is true, and false otherwise. If one of the expressions is NULL, then it returns NULL.
The syntax for the XOR operator in SQL is given by:
SELECT expression1 XOR expression2;
The table below summarizes the results when comparing boolean expressions using the XOR operator.
XOR
OperatorTrue | False | NULL | |
---|---|---|---|
True | False | True | NULL |
False | True | False | NULL |
NULL | NULL | NULL | False |
In summary, it is important that you learn and understand how logical operators and their tables work to guarantee correct results in your code.
A guide with details about additional operators can be found here: SQL Logical Operators