Database Tables and Keys · Lesson 11 of 112

Database NULL and Missing Values

What you will learn

  • What NULL means and what it is not
  • Why = NULL never works, and what to write instead
  • How NULL affects counting, averages and comparisons
  • How to replace NULL with COALESCE

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It creates a small table of its own in your browser's copy; run the examples from top to bottom, and press Reset data to start again.

1. What NULL is

Real data has gaps: a customer's phone is unknown, a person has no middle name, an order has not shipped yet. NULL is the database's marker for "no value here". It can mean unknown (there is a phone, we do not have it) or not applicable (there is no middle name).

NULL is not:

  • the number 0 (zero marks is an answer; NULL means no answer),
  • an empty text '',
  • the text 'NULL'.

Four people answer a form. Rina and Sam gave real values (Sam's 0 and '' are real), Lee left the age and phone empty, and Amy typed the four letters NULL as her phone:

CREATE TABLE survey_answers (name TEXT, age INTEGER, phone TEXT);
INSERT INTO survey_answers VALUES ('Rina', 25, '0171'), ('Sam', 0, ''), ('Lee', NULL, NULL), ('Amy', 31, 'NULL');
SELECT name, age, typeof(age), phone, typeof(phone) FROM survey_answers;
name   age  typeof(age)  phone  typeof(phone)
----  ----  -----------  -----  -------------
Rina    25  integer      0171   text
Sam      0  integer             text
Lee   NULL  null         NULL   null
Amy     31  integer      NULL   text
(4 rows)
CREATE TABLE kyc_forms (name TEXT, age INTEGER, phone TEXT);
INSERT INTO kyc_forms VALUES ('Rina', 25, '0171'), ('Sam', 0, ''), ('Lee', NULL, NULL), ('Amy', 31, 'NULL');
SELECT name, age, typeof(age), phone, typeof(phone) FROM kyc_forms;
name   age  typeof(age)  phone  typeof(phone)
----  ----  -----------  -----  -------------
Rina    25  integer      0171   text
Sam      0  integer             text
Lee   NULL  null         NULL   null
Amy     31  integer      NULL   text
(4 rows)
CREATE TABLE contact_forms (name TEXT, age INTEGER, phone TEXT);
INSERT INTO contact_forms VALUES ('Rina', 25, '0171'), ('Sam', 0, ''), ('Lee', NULL, NULL), ('Amy', 31, 'NULL');
SELECT name, age, typeof(age), phone, typeof(phone) FROM contact_forms;
name   age  typeof(age)  phone  typeof(phone)
----  ----  -----------  -----  -------------
Rina    25  integer      0171   text
Sam      0  integer             text
Lee   NULL  null         NULL   null
Amy     31  integer      NULL   text
(4 rows)

Sam's age 0 and phone '' are real values; Lee's age and phone are NULL; Amy's phone is the four-letter text 'NULL', a trap to avoid.

The sample tables have real gaps too:

SELECT COUNT(*) AS customers, COUNT(phone) AS with_phone, COUNT(*) - COUNT(phone) AS phone_is_null FROM customers;
customers  with_phone  phone_is_null
---------  ----------  -------------
      420         336             84
(1 row)
SELECT COUNT(*) AS audit_rows, COUNT(note) AS with_note, COUNT(*) - COUNT(note) AS note_is_null FROM audit_log;
audit_rows  with_note  note_is_null
----------  ---------  ------------
       120         91            29
(1 row)
SELECT COUNT(*) AS enrollments, COUNT(grade) AS graded, COUNT(*) - COUNT(grade) AS grade_is_null FROM enrollments;
enrollments  graded  grade_is_null
-----------  ------  -------------
       1002     809            193
(1 row)

2. Comparisons with NULL

Because NULL means "unknown", comparing it to anything gives "unknown", not true or false:

SELECT NULL = NULL, NULL = 5, NULL <> 5, NULL = 0;
NULL = NULL  NULL = 5  NULL <> 5  NULL = 0
-----------  --------  ---------  --------
NULL         NULL      NULL       NULL
(1 row)
SELECT NULL = NULL, NULL = 5, NULL <> 5, NULL = 0;
NULL = NULL  NULL = 5  NULL <> 5  NULL = 0
-----------  --------  ---------  --------
NULL         NULL      NULL       NULL
(1 row)
SELECT NULL = NULL, NULL = 5, NULL <> 5, NULL = 0;
NULL = NULL  NULL = 5  NULL <> 5  NULL = 0
-----------  --------  ---------  --------
NULL         NULL      NULL       NULL
(1 row)

Every answer is NULL ("unknown"). Is an unknown value equal to another unknown value? We cannot say. This is why WHERE age = NULL finds nothing. Use IS NULL and IS NOT NULL:

SELECT name FROM survey_answers WHERE age = NULL;
SELECT name FROM survey_answers WHERE age IS NULL;
SELECT name FROM survey_answers WHERE age IS NOT NULL;
name
----
(0 rows)

name
----
Lee
(1 row)

name
----
Rina
Sam
Amy
(3 rows)
SELECT name FROM kyc_forms WHERE age = NULL;
SELECT name FROM kyc_forms WHERE age IS NULL;
SELECT name FROM kyc_forms WHERE age IS NOT NULL;
name
----
(0 rows)

name
----
Lee
(1 row)

name
----
Rina
Sam
Amy
(3 rows)
SELECT name FROM contact_forms WHERE age = NULL;
SELECT name FROM contact_forms WHERE age IS NULL;
SELECT name FROM contact_forms WHERE age IS NOT NULL;
name
----
(0 rows)

name
----
Lee
(1 row)

name
----
Rina
Sam
Amy
(3 rows)

The surprise in "not equal"

SELECT name FROM survey_answers WHERE age <> 25;
name
----
Sam
Amy
(2 rows)
SELECT name FROM kyc_forms WHERE age <> 25;
name
----
Sam
Amy
(2 rows)
SELECT name FROM contact_forms WHERE age <> 25;
name
----
Sam
Amy
(2 rows)

Lee has no age, so "is Lee's age different from 25?" is unknown, and rows are kept only when the test is true. Lee is missing from the answer even though you might expect him. To include him, say so:

SELECT name FROM survey_answers WHERE age <> 25 OR age IS NULL;
name
----
Sam
Lee
Amy
(3 rows)
SELECT name FROM kyc_forms WHERE age <> 25 OR age IS NULL;
name
----
Sam
Lee
Amy
(3 rows)
SELECT name FROM contact_forms WHERE age <> 25 OR age IS NULL;
name
----
Sam
Lee
Amy
(3 rows)

3. NULL and counting

Aggregate functions skip NULLs (except COUNT(*), which counts rows):

SELECT COUNT(*), COUNT(age), SUM(age), AVG(age) FROM survey_answers;
COUNT(*)  COUNT(age)  SUM(age)            AVG(age)
--------  ----------  --------  ------------------
       4           3        56  18.666666666666668
(1 row)
SELECT COUNT(*), COUNT(age), SUM(age), AVG(age) FROM kyc_forms;
COUNT(*)  COUNT(age)  SUM(age)            AVG(age)
--------  ----------  --------  ------------------
       4           3        56  18.666666666666668
(1 row)
SELECT COUNT(*), COUNT(age), SUM(age), AVG(age) FROM contact_forms;
COUNT(*)  COUNT(age)  SUM(age)            AVG(age)
--------  ----------  --------  ------------------
       4           3        56  18.666666666666668
(1 row)

There are 4 rows, but only 3 ages (25, 0 and 31), so COUNT(age) is 3, the sum is 56 and the average is 56 / 3, not 56 / 4. If NULL should count as zero, say so (next section).

4. COALESCE: a fallback value

COALESCE(a, b, ...) returns the first value that is not NULL:

SELECT name, COALESCE(age, 0) AS age_or_zero, COALESCE(phone, 'no phone') AS phone_or_text FROM survey_answers;
SELECT AVG(COALESCE(age, 0)) AS average_with_zero FROM survey_answers;
SELECT first_name, COALESCE(phone, 'no phone') AS phone FROM customers ORDER BY customer_id LIMIT 5;
name  age_or_zero  phone_or_text
----  -----------  -------------
Rina           25  0171
Sam             0
Lee             0  no phone
Amy            31  NULL
(4 rows)

average_with_zero
-----------------
             14.0
(1 row)

first_name  phone
----------  ------------
Vik         +35 49854883
Wanda       no phone
Dana        +58 13864688
Noah        +48 26805141
Hugo        +49 13135594
(5 rows)
SELECT name, COALESCE(age, 0) AS age_or_zero, COALESCE(phone, 'no phone') AS phone_or_text FROM kyc_forms;
SELECT AVG(COALESCE(age, 0)) AS average_with_zero FROM kyc_forms;
SELECT log_id, COALESCE(note, 'no note') AS note FROM audit_log ORDER BY log_id LIMIT 5;
name  age_or_zero  phone_or_text
----  -----------  -------------
Rina           25  0171
Sam             0
Lee             0  no phone
Amy            31  NULL
(4 rows)

average_with_zero
-----------------
             14.0
(1 row)

log_id  note
------  --------------
     1  no note
     2  no note
     3  limit raised
     4  status changed
     5  status changed
(5 rows)
SELECT name, COALESCE(age, 0) AS age_or_zero, COALESCE(phone, 'no phone') AS phone_or_text FROM contact_forms;
SELECT AVG(COALESCE(age, 0)) AS average_with_zero FROM contact_forms;
SELECT student_id, COALESCE(guardian_name, 'unknown') AS guardian FROM students ORDER BY student_id LIMIT 5;
name  age_or_zero  phone_or_text
----  -----------  -------------
Rina           25  0171
Sam             0
Lee             0  no phone
Amy            31  NULL
(4 rows)

average_with_zero
-----------------
             14.0
(1 row)

student_id  guardian
----------  -----------
         1  Amir Khan
         2  Nadia Haque
         3  Mina Garcia
         4  Femi Meyer
         5  Ana Torres
(5 rows)

Now the average is 56 / 4 = 14.0, because Lee's missing age was counted as 0. Whether that is right depends on the question. A missing age is not really zero, so the earlier average (18.67) may be the honest one. Decide on purpose, not by accident.

5. Sorting NULLs

SQLite treats NULL as smaller than any value, so it comes first in ascending order:

SELECT name, age FROM survey_answers ORDER BY age;
name   age
----  ----
Lee   NULL
Sam      0
Rina    25
Amy     31
(4 rows)
SELECT name, age FROM kyc_forms ORDER BY age;
name   age
----  ----
Lee   NULL
Sam      0
Rina    25
Amy     31
(4 rows)
SELECT name, age FROM contact_forms ORDER BY age;
name   age
----  ----
Lee   NULL
Sam      0
Rina    25
Amy     31
(4 rows)

Other systems may place NULLs differently by default; check the documentation of the system you use.

Worked example: when is NULL meaningful?

A table of deliveries. A row with delivered_on NULL means "not delivered yet". The question "which deliveries are still waiting?" is a test for NULL:

CREATE TABLE deliveries (id INTEGER, address TEXT, delivered_on TEXT);
INSERT INTO deliveries VALUES (1, '12 Lake Road', '2026-03-02'), (2, '5 Hill Street', NULL), (3, '9 Park Lane', '2026-03-04'), (4, '3 Mill Close', NULL);
SELECT id FROM deliveries WHERE delivered_on IS NULL ORDER BY id;
id
--
 2
 4
(2 rows)

A table of cheques. A row with cleared_on NULL means "not cleared yet". The question "which cheques are still waiting?" is a test for NULL:

CREATE TABLE cheques (id INTEGER, payee TEXT, cleared_on TEXT);
INSERT INTO cheques VALUES (1, 'Lake Road Ltd', '2026-03-02'), (2, 'Hill Street Co', NULL), (3, 'Park Lane Shop', '2026-03-04'), (4, 'Mill Close Bakery', NULL);
SELECT id FROM cheques WHERE cleared_on IS NULL ORDER BY id;
id
--
 2
 4
(2 rows)

A table of library loans. A row with returned_on NULL means "not returned yet". The question "which books are still out?" is a test for NULL:

CREATE TABLE library_loans (id INTEGER, book TEXT, returned_on TEXT);
INSERT INTO library_loans VALUES (1, 'River Stories', '2026-03-02'), (2, 'Hill Maths', NULL), (3, 'Park Poems', '2026-03-04'), (4, 'Mill Science', NULL);
SELECT id FROM library_loans WHERE returned_on IS NULL ORDER BY id;
id
--
 2
 4
(2 rows)

Using NULL here is meaningful: the absence of a date is the information. Do not invent a fake date such as 1900-01-01 to mean "none"; reports would treat it as a real date. The tables are made up.

Common mistakes

  • = NULL instead of IS NULL.
  • Using 0, '' or 'N/A' for "unknown", which then pollute sums, averages and sorting.
  • Forgetting NULL in "not equal" conditions, so rows silently disappear.
  • Averages that skip NULLs unnoticed. Know whether missing should count as zero.

Key takeaways

  • NULL means "no value": unknown or not applicable. It is not 0 or an empty text.
  • Any comparison with NULL is unknown, so use IS NULL / IS NOT NULL.
  • COUNT(column), SUM and AVG skip NULLs; COUNT(*) counts rows.
  • COALESCE(x, fallback) replaces NULL with a value.
  • Allow NULL only where "no value" is a real, meaningful state.

Practice

1. What is wrong with SELECT * FROM people WHERE phone = NULL?

Show answer

A comparison with NULL is never true, so it returns no rows. Use WHERE phone IS NULL.

2. Is a quiz mark of 0 the same as NULL?

Show answer

No. 0 is a real mark; NULL means no mark was recorded.

3. A table has 10 rows and 7 of them have an age. What do COUNT(*) and COUNT(age) return?

Show answer

COUNT(*) returns 10 and COUNT(age) returns 7.

4. What does COALESCE(NULL, NULL, 'x') return?

Show answer

'x', the first value that is not NULL.

5. Why might it be wrong to replace missing ages with 0 before averaging?

Show answer

Zero is a real age value and pulls the average down; a missing age is unknown, not zero.

6. Give an example where NULL is a good choice.

Show answer

For example a delivered_on date for an order that has not been delivered yet: no value is the true state.