A NULL value can be used to express the following:
The corresponding value exists in principle but is unknown temporarily or permanently for the row at hand (such as the birthday of a person).
The corresponding value does not exist, for example the owner of an unregistered car.
NULL is a special value in SQL.
For numeric datatypes, NULL does not equal 0.
For string datatypes, NULL does not equal an empty string or a space character.
NULL Values: Origin
NULL values can result in the following cases:
When reading rows (the SELECT statement).
If the table queried from already contains NULL values.
When using outer joins.
When inserting or updating rows (INSERT and UPDATE statements).
If NULLs are permitted in a column, no value for that column is provided, and no default value is defined.
If a NULL value is explicitly specified for a column and NULL values are allowed.
When adding a column to a table (ALTER TABLE statement). If the table has at least one row, NULL values are allowed for the added column and no default value is defined for the added column.
NULL Values Lead to Three-Value Logic
By allowing NULL values, a third truth value is necessary. In addition to TRUE and FALSE there is also the truth value UNKNOWN in SQL. Any comparison with a NULL value other than the IS NULL or IS NOT NULL predicates results in the truth value UNKNOWN.
This applies to all value-based comparison operators: =, >, >=, <, <=, <>
| A | A = 7 | A <> 7 | A >= 6 |
|---|---|---|---|
| 5 | false | true | false |
| 7 | true | false | true |
| NULL | unknown | unknown | unknown |
Three-Value Logic Extends Logical Operators: AND and OR
Two-value logic is a subset of three-value logic. When NULL values are eliminated, the AND test simplifies.
| X | Y | X AND Y |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
| true | unknown | unknown |
| false | unknown | false |
| unknown | true | unknown |
| unknown | false | false |
| unknown | unknown | unknown |
When NULL values are eliminated, the OR test also simplifies.
| X | Y | X AND Y |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
| true | unknown | true |
| false | unknown | unknown |
| unknown | true | true |
| unknown | false | unknown |
| unknown | unknown | unknown |
The introduction of the logical value UNKNOWN and the way expressions are evaluated leads to an unexpected result. That is that logical expressions that always evaluate to true in usual programming languages and according to common sense no longer evaluate to TRUE in all cases. The following list shows some examples:
- A = A
- (A<5) OR (A>=5)
- 0*A = 0
- 2*A = A+A
- A OR (NOT A)
- MAX(...) >= ALL (<set of all values>)
- MIN(...) <= ALL (<set of all values>)
Do these Queries have the Same Result?
Do the following two SQL queries have the same result?
- Code Snippet123SELECT Name, Overtime FROM Official WHERE Overtime <= 10 OR Overtime > 10;
- Code Snippet12SELECT Name, Overtime FROM Official;
The Queries May Have Different Results!
The two SQL queries have different results!
Only officials where OvertimeIS NOT NULL applies:
Code Snippet123SELECT Name, Overtime FROM Official WHERE Overtime <= 10 OR Overtime > 10;NAME OVERTIME Mr A 10 Mr B 10 Ms C 20 Mr E 10 Mr F 18 Ms G 22 All officials (including those with: Overtime IS NULL )
Code Snippet12SELECT Name, Overtime FROM Official;NAME OVERTIME Mr A 10 Mr B 10 Ms C 20 Ms D ? Mr E 10 Mr F 18 Ms G 22 Ms H ? Mr I ?