0% found this document useful (0 votes)
44 views8 pages

Week 2 - Nulls

This document discusses NULL values in SQL. NULL represents missing or unknown data that can occur in any data type. NULL propagates through operations, causing the result to be NULL. SQL uses three-valued logic for NULL, where comparisons with NULL return unknown rather than true or false. To check for NULL, the IS operator is used rather than equality operators.

Uploaded by

rasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

Week 2 - Nulls

This document discusses NULL values in SQL. NULL represents missing or unknown data that can occur in any data type. NULL propagates through operations, causing the result to be NULL. SQL uses three-valued logic for NULL, where comparisons with NULL return unknown rather than true or false. To check for NULL, the IS operator is used rather than equality operators.

Uploaded by

rasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROG8080

Week 2 – NULLs
Tim Welsh
• Data in SQL can be represented by a NULL
• NULLs can occur in any data type

SELECT CAST(NULL AS INT)


SELECT CAST(NULL AS VARCHAR)
SELECT CAST(NULL AS DATE)

NULL • NULLs are used to represent missing data (it’s


value denotes the absence of a value)
• It is important to note that the absence of a value
is treated differently than a value.
• In SQL, it is sometimes better to think of NULL as a
state instead of a value
• (Codd 1990) There are two types of missing
data:
• Missing and Applicable
• E.g middle names; not everyone has a middle
Types of name… NULL means non-existent
• Missing but Inapplicable
Missing • E.g surnames; everyone should have one… NULL
means missed measurement
Data • In SQL there is no distinction between these types
of missing data (although people debate if it
should): All NULLs are treated equally in SQL.
• This makes understanding the context all the more
important when analyzing data with NULLs.
• How does NULL behave with other types?
• Generally, a NULL propagates through and
causes the entire result to be NULL

NULL SELECT 3 + NULL

propagatio SELECT 'abc' + NULL


SELECT DATEDIFF(YEAR, NULL, GETDATE())
n
• Later, we will revisit how NULLs are handled in
aggregate functions e.g SUM(), COUNT(), …
• Many times, we want to check if our data
contains NULL values and use the more familiar
Checking 2-values logic (with just TRUE and FALSE)

for NULLs • Using the = operator is insufficient since `Anything =


NULL` evaluates to Unknown
with IS • Fortunately, there is a separate operator IS that
allows us to check for NULLs in a way that evaluates
to either T
• SELECT 'hello world' WHERE 1 = 1
• -- 1=1 evaluates to true so we get the selected result

• SELECT 'hello world' WHERE 1 = 2


• -- 1=2 evaluates to false so we don't get the selected
result

Null • SELECT 'hello world' WHERE 1 = NULL

Checks • -- 1=NULL evaluates to unknown; we don't get the selected


result

(Examples) • SELECT 'hello world' WHERE NULL = NULL


• -- NULL=NULL evaluates to unknown; we don't get the selected
result

• SELECT 'hello world' WHERE NULL IS NULL


• -- NULL IS NULL evaluates to true; we get the selected
result
SELECT 'hello world' WHERE 1 IS NOT NULL
-- we can use IS NOT to negate the IS clause

SELECT 'hello world' WHERE NOT (1 = NULL)


Null SELECT 'hello world' WHERE NOT (NOT (1 = NULL))

Checks
-- the negation of an Unknown is still Unknown (and
won't be returned by WHERE clause)

(Examples) SELECT 'hello world' WHERE NOT(1=2 AND 1=NULL)


-- how do true, false, unknown play together?
-- can play with SQL doing things like above to find
out (or just find the truth table)

This all probably seems silly now, but it has implications when
we start querying large tables with potentially many NULLs
• You should be able to answer the
following questions:
• What are the two “types of missing data”?
At this • How do NULLs propagate through basic operations
in SQL?
point… • What does it mean when we say SQL has a three
valued logic system?
• In SQL, what is the proper operator to check if a
value is NULL?
• How can I determine if some logical expression
evaluates to unknown?

You might also like