Sub topic 19
NULLs and three-valued logic
This chapter assumes you can read SELECT-FROM-WHERE queries, know what subqueries and GROUP BY do, and remember the propositional operators AND, OR and NOT. It explains why SQL has three truth values and where that changes query results.
What NULL means
NULL is a marker for a missing or unknown value. It is not zero, not an empty string, and not equal to anything, including another NULL. A comparison that involves NULL therefore cannot be answered with true or false.
SQL handles this with a third truth value, UNKNOWN. Any comparison (=, <>, <, >, and so on) with NULL on either side evaluates to UNKNOWN. This includes NULL = NULL and NULL <> NULL.
Three-valued truth tables
The logical operators extend to UNKNOWN as follows. Read UNKNOWN as "could be either".
| A | B | A AND B | A OR B |
|---|---|---|---|
| TRUE | UNKNOWN | UNKNOWN | TRUE |
| FALSE | UNKNOWN | FALSE | UNKNOWN |
| UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN |
NOT TRUE is FALSE, NOT FALSE is TRUE, and NOT UNKNOWN is UNKNOWN. You can check the table by substituting both possibilities for UNKNOWN. FALSE AND x is FALSE for either value of x, so the result is certain. TRUE AND x depends on x, so the result is UNKNOWN.
Where UNKNOWN is discarded
A WHERE clause keeps a row only when the condition is TRUE. Rows where the condition is FALSE or UNKNOWN are both dropped. HAVING behaves the same way for groups.
CHECK constraints work the other way. A row is rejected only when the condition is FALSE, so UNKNOWN passes. CHECK (age > 0) accepts a row whose age is NULL.
Counting with NULLs
Suppose Student has three rows with ages 18, 21 and NULL.
CREATE TABLE Student (sid INT PRIMARY KEY, sname VARCHAR(20), age INT);
INSERT INTO Student VALUES (1, 'Asha', 18), (2, 'Ben', 21), (3, 'Chen', NULL);
SELECT COUNT(*) FROM Student; -- 3
SELECT COUNT(age) FROM Student; -- 2
SELECT AVG(age) FROM Student; -- 19.5
SELECT * FROM Student WHERE age > 20; -- Ben
SELECT * FROM Student WHERE NOT (age > 20); -- Asha
SELECT * FROM Student WHERE age > 20 OR age <= 20; -- Asha, Ben
SELECT * FROM Student WHERE age IS NULL; -- ChenThe Chen row appears in neither the condition nor its negation, because both evaluate to UNKNOWN for it. The third query shows that p OR NOT p is not a tautology in SQL. The row with a NULL age is lost.
To test for NULL, use IS NULL or IS NOT NULL. Writing age = NULL is always UNKNOWN and returns no rows.
Aggregates and grouping
COUNT(*)counts rows.COUNT(col)counts rows wherecolis not NULL.COUNT(DISTINCT col)counts distinct non-NULL values.SUM,AVG,MINandMAXskip NULLs.AVGdivides by the number of non-NULL values, which is why the average above is 19.5, not 13.- Over an empty input, or a column that is entirely NULL,
SUM,AVG,MINandMAXreturn NULL.COUNTreturns 0. GROUP BYputs all NULLs in one group, andDISTINCTcollapses NULLs into one value. These are the places where SQL treats two NULLs as the same, even though=does not.UNION,INTERSECTandEXCEPTalso compare rows with NULLs as equal when removing duplicates.
Joins
An inner join with ON s.sid = e.sid never matches rows where either side is NULL, because the comparison is UNKNOWN. An outer join pads unmatched rows with NULLs. Those padded NULLs then behave like any other NULL in later conditions. A WHERE condition such as e.cid > 5 on the padded side silently removes the padded rows and turns the outer join into an inner join.
The NOT IN trap
x NOT IN (a, b, c) expands to x <> a AND x <> b AND x <> c. If any listed value is NULL, one of the conjuncts is UNKNOWN, so the whole condition can never be TRUE.
CREATE TABLE T (x INT);
CREATE TABLE U (y INT);
INSERT INTO T VALUES (1), (2);
INSERT INTO U VALUES (1), (NULL);
SELECT x FROM T WHERE x NOT IN (SELECT y FROM U);
-- empty result: for x = 2, (2<>1) AND (2<>NULL) is TRUE AND UNKNOWN = UNKNOWN
SELECT x FROM T WHERE NOT EXISTS (SELECT 1 FROM U WHERE U.y = T.x);
-- returns 2: EXISTS tests whether any row is returned, so NULL never causes UNKNOWNIN is less surprising. 2 IN (1, NULL) is 2 = 1 OR 2 = NULL, which is FALSE OR UNKNOWN, which is UNKNOWN. A row is still never kept by a false match, but a true match (1 IN (1, NULL)) gives TRUE.
Quantified comparisons follow the same logic. x > ALL (subquery) is TRUE for an empty subquery, but a single NULL in a non-empty subquery makes the result UNKNOWN unless some other value already makes it FALSE.
Constraints and NULL
- A primary key column cannot be NULL.
- A UNIQUE constraint normally allows several NULLs, because NULLs are not equal to each other. Some systems differ, so state the assumption in an exam answer only if the question gives one.
- A foreign key with a NULL value is not a referential integrity violation. The reference is simply absent.
GATE strategies for NULL questions
- Mark each row's condition as TRUE, FALSE or UNKNOWN, then keep only TRUE rows.
- For a query and its negation, expect the NULL rows to appear in neither. Their combined count can be less than the table size.
- For an aggregate, decide first whether it counts rows or non-NULL values.
- For NOT IN, look for a NULL in the subquery before anything else.
- For maximum and minimum counts, try the all-NULL case for the minimum.
Exercise
Student has ages 18, 21, NULL and 25. How many rows does SELECT * FROM Student WHERE age > 20 OR age <= 20 return, and what does SELECT AVG(age) FROM Student return?
Show answer
The WHERE clause returns 3 rows. The rows with 18, 21 and 25 each make one side TRUE, while the NULL row gives UNKNOWN OR UNKNOWN, which is UNKNOWN and is dropped. AVG skips the NULL, so it returns (18 + 21 + 25) / 3, which is about 21.33.
Exercise
Table R(a) contains the values 1, 2 and NULL. What does SELECT COUNT(*) FROM R WHERE a NOT IN (SELECT a FROM R WHERE a = 1) return?
Show answer
It returns 1. The subquery yields only the value 1, with no NULL in it. For a = 2, the condition 2 <> 1 is TRUE, so that row is kept. For a = 1 it is FALSE, and for a NULL it is UNKNOWN, so both are dropped.