7 SQL NULL
Coding
SQL
Database
Join
NULL
This lecture discusses the concept of NULL values in SQL, including how to represent missing information and inapplicable attributes. It also covers how to check for NULL values and the impact of NULL values on arithmetic expressions, comparison operators, and aggregation.
Null values in SQL
- Missing Information:
- Missing value.
- E.g., we know a student has some email address, but we don’t know what it is.
- Inapplicable attribute.
- E.g., the value of attribute spouse for a person who is single.
- Missing value.
- Representing missing information:
- One possibility: use a special value as a placeholder. E.g.,
- If age unknown, use -1.
- If StNum unknown, use 999999999.
- Pros and cons?
- Better solution: use a value not in any domain. We call this a null value.
- Tuples in SQL relations can have NULL as a value for one or more components.
- One possibility: use a special value as a placeholder. E.g.,
- Check for
NULL
values- You can compare an attribute value to
NULL
withIS NULL
IS NOT NULL
- You can compare an attribute value to
Tip 1: Example: Check for NULL values
SELECT *
FROM Course
WHERE breadth IS NULL;
- Note: do not use WHERE breadth = NULL;
Impact of Null Values
- Assume \(x\) is
NULL
- Arithmetic expression: Result is always
NULL
Tip 2: Example: Arithmetic with NULL
\[x+\texttt{grade}=\texttt{NULL}\] \[x*0=\texttt{NULL}\] \[x-x=\texttt{NULL}\]
- Comparison operators (\(>\), \(<\), \(=\), \(\dots\)): Result is
UNKNOWN
(neitherTRUE
norFALSE
)
Tip 3: Example: Comparison with NULL
\[x<32 \quad\texttt{ --> UNKNOWN}\]
- This
UNKNOWN
is a truth-value - Truth-values in SQL are:
TRUE
,FALSE
,UNKNOWN
(a 3-value truth value system!)- Logic with
UNKNOWN
: \[\begin{aligned} \texttt{UNKNOWN} \lor\texttt{FALSE}&\equiv\texttt{UNKNOWN}\\ \texttt{UNKNOWN} \lor\texttt{TRUE}&\equiv\texttt{TRUE}\\ \texttt{UNKNOWN} \land\texttt{FALSE}&\equiv\texttt{FALSE}\\ \texttt{UNKNOWN} \land\texttt{TRUE}&\equiv\texttt{UNKNOWN}\\ \neg\texttt{UNKNOWN}&\equiv\texttt{UNKONWN} \end{aligned}\] - A tuple is in a query result \(\iff\) the result of the
WHERE
clause isTRUE
.
- Logic with
Impact of Null Values on Aggregation
- “Aggregation ignores
NULL
.”- NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column (unless every value is
NULL
).
- NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column (unless every value is
- If ALL values are
NULL
in a column, then the result of the aggregation is NULL. - Exception:
COUNT
of an empty set is 0. (think ofCOUNT
(columnName) as a function that counts the non-null values in that column.)
Tip 4: Example: Aggregation with NULL
- R&S&T are defined as:
R | S | T
x | x | x
----- | ----- | -----
NULL | NULL |
1 | |
COUNT()
COUNT(R.*)=2
andCOUNT(R.x)=1
COUNT(S.*)=1
andCOUNT(S.x)=0
COUNT(T.*)=0
andCOUNT(T.x)=0
- Other aggregates:
MIN(R.x)=1
andMAX(R.x)=1
MIN(S.x)=NULL
andMAX(S.x)=NULL
MIN(T.x)=NULL
andMAX(T.x)=NULL
NULL in Set Operations
NULL
is treated differently by the set operatorsUNION
,EXCEPT
, andINTERSECT
than it is in search conditions.- When comparing rows, set operators treat
NULL
values as equal to each other. - In contrast, when
NULL
is compared toNULL
in a search condition the result isUNKNOWN
(not true).