Saturday, April 5, 2014

Fun with NULL

I hope the example below will help you understand more about NULL. This 4 letters word can be troublesome and fun. After reading about null of this document, and this blog post by Hugo Kornelis, I now conclude NULL is not unknown and update my explanation below.

Definition of NULL
   NULL is not a value
   NULL is a mark for a missing value - not a value itself.
   NULL represent missing data
   NULL is not unknown
   NULL is not applicable
   NULL isn't empty
   NULL doesn't equal zero or an empty string
   NULL is not the value of string 'NULL'
   NULL doesn't equal to NULL (just like saying something is missing can't equal to another missing object. I'll explain more on this below.)
   Also SQL uses 3-valued predicate logic: True, False, Unknown. 

March 27, 2014 SSC question of the day: Guess what this query returns?
Select 1 Where 'hello'=Null
Union
Select 2 Where 'hello'<>Null
Union
Select 3 Where 'hello' Is Null
Union
Select 4 Where 'hello' Is Not Null
Union
Select 5 Where Not ('hello'=Null)

Below is my explanation for each SELECT statement for the query above.
Note, from this question
'hello' is known
NULL represent missing data

/* Question: Does a known object equals to a missing object?
   Answer:   unknown. Because NULL represents missing data (a string value, a house, a car, dollar amount). 
   Let's assume the missing data value could be 'hi', $200.00, 'hello'.
   does 'hello'='hi'?           no
   does 'hello'=$200.00?  no
   does 'hello'='hello'?      yes
   So we cannot be 100% sure that a known object can be equaled to a missing object. 
   Therefore the 'hello'=null condition is unknown.
   Thus this set is filtered out */
Select 1 Where 'hello'=Null 
Union
/* Question: Does a known object doesn't equals to a missing object?
   Answer:   unknown. Because NULL represents missing data (a string value, a house, a car, dollar amount).
    Let's assume  the missing data value could be 'hi', $200.00, 'hello'.
   does 'hello'<>'hi'?           yes
   does 'hello'<>$200.00?  yes
   does 'hello'<>'hello'?      no
   So we cannot be 100% sure that a known object doesn't equal to a missing object. 
   Therefore the 'hello'<>null condition is unknown.
   Thus this set is filtered out */
Select 2 Where 'hello'<>Null
Union
/* Question: Does a known object is missing?
   Answer:   False. Because we known exactly what is the value of the string literal 'hello' is.
   Therefore the 'hello' Is Null condition is false.
   Thus this set is filtered out */
Select 3 Where 'hello' Is Null
Union
/* Question: Does a known object is not missing?
   Answer: True. Because we known exactly what is the value of the string literal 'hello' is, so it's not unknown or missing.
   Therefore the 'hello' Is Not Null condition is true.
   Thus this set is returned. */
Select 4 Where 'hello' Is Not Null
Union
/* From the explanation of the first select statement above,
   we can conclude that the condition 'hello'=Null is unknown.
   Thus Not ('hello'=Null) is equivalent to Not(unknown).
   Not(unknown) is evaluated to unknown. 
   Thus this set is filtered out */
Select 5 Where Not ('hello'=Null)
Union
/* I throw in this last select to make thing more clear.
   From the explanation of the third select statement above,
   we can conclude that the condition 'hello' Is Null is false.
   Thus Not ('hello' Is Null) is equivalent to Not(false).
   Not(false) is evaluated to true. 
   Thus this set is returned. */
Select 6 Where Not ('hello' Is Null);

Result for SSC question: 
4

Result for the query above:
4
6

Notice that if a condition is false or unknown the set won't be included in the result. Please provide any comment, suggestion or correction for this post if you believe any information above is not correct.
If you want to learn more about NULL, please read 4 parts series about NULL by Hugo Kornelis here.

No comments:

Post a Comment