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
NULLvalues- You can compare an attribute value to
NULLwithIS NULLIS 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(neitherTRUEnorFALSE)
Tip 3: Example: Comparison with NULL
\[x<32 \quad\texttt{ --> UNKNOWN}\]
- This
UNKNOWNis 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
WHEREclause 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
NULLin a column, then the result of the aggregation is NULL. - Exception:
COUNTof 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.*)=2andCOUNT(R.x)=1COUNT(S.*)=1andCOUNT(S.x)=0COUNT(T.*)=0andCOUNT(T.x)=0
- Other aggregates:
MIN(R.x)=1andMAX(R.x)=1MIN(S.x)=NULLandMAX(S.x)=NULLMIN(T.x)=NULLandMAX(T.x)=NULL
NULL in Set Operations
NULLis treated differently by the set operatorsUNION,EXCEPT, andINTERSECTthan it is in search conditions.- When comparing rows, set operators treat
NULLvalues as equal to each other. - In contrast, when
NULLis compared toNULLin a search condition the result isUNKNOWN(not true).