TExcelTAD
Home › SQL › SQL NULL Values

SQL NULL Values

NULL means "no value" / "unknown". It is not zero, not an empty string and not 'NULL' — it is the absence of a value. In TAD Market, four customers have no e-mail address, and orders that have not shipped yet have no shipped_date.

Testing for NULL

You cannot find NULLs with =. Any comparison with NULL — even NULL = NULL — is unknown, and WHERE drops unknown rows. Use IS NULL and IS NOT NULL instead:

sql · try it yourself
SELECT customer_id, name, email
FROM customers
WHERE email IS NULL
ORDER BY customer_id;
customer_id | name            | email
------------+-----------------+------
5           | Le Petit Marche | NULL
12          | Omar Murphy     | NULL
18          | Felix Reyes     | NULL
23          | Zara Ali        | NULL
(4 rows)
sql · try it yourself
-- The wrong way: returns nothing
SELECT COUNT(*) AS wrong FROM customers WHERE email = NULL;

-- The right way
SELECT COUNT(*) AS has_email FROM customers WHERE email IS NOT NULL;
wrong
-----
0
(1 row)

has_email
---------
21
(1 row)

NULL in expressions

Most operations involving NULL produce NULL:

sql · try it yourself
SELECT 5 + NULL     AS plus,
       'abc' || NULL AS concat,
       NULL = NULL  AS equal,
       NULL IS NULL AS is_null,
       NULL > 3     AS greater;
plus | concat | equal | is_null | greater
-----+--------+-------+---------+--------
NULL | NULL   | NULL  | 1       | NULL
(1 row)

(SQLite shows true as 1 and false as 0.)

Unshipped orders

sql · try it yourself
SELECT status, COUNT(*) AS orders
FROM orders
WHERE shipped_date IS NULL
GROUP BY status
ORDER BY status;
status     | orders
-----------+-------
cancelled  | 7
processing | 8
(2 rows)

Every processing and cancelled order has a NULL shipped_date — exactly what we would expect.

NULLs and aggregate functions

Aggregates such as COUNT(column), SUM and AVG skip NULLs. COUNT(*) counts rows, whatever their content:

sql · try it yourself
SELECT COUNT(*)            AS all_customers,
       COUNT(email)        AS with_email,
       COUNT(*) - COUNT(email) AS without_email
FROM customers;
all_customers | with_email | without_email
--------------+------------+--------------
25            | 21         | 4
(1 row)

Replacing NULL for display

COALESCE(a, b, …) returns its first non-NULL argument — great for showing a placeholder:

sql · try it yourself
SELECT name, COALESCE(email, '(no email)') AS email
FROM customers
WHERE country IN ('Italy', 'India', 'Mexico', 'Turkey', 'Ireland')
ORDER BY name;
name            | email
----------------+-------------------------
Felix Reyes     | (no email)
Isla Brennan    | isla.brennan@example.com
Le Petit Marche | (no email)
Omar Murphy     | (no email)
Zara Ali        | (no email)
(5 rows)

The NULL Functions lesson covers COALESCE, IFNULL and NULLIF in detail.

Summary

Do Don't
email IS NULL email = NULL
email IS NOT NULL email <> NULL
COALESCE(email, '-') to display a default assume NULL behaves like '' or 0

Note: SQLite also has IS and IS NOT for any value: a IS b is true when both are NULL or both are equal. The standard spelling is a IS NOT DISTINCT FROM b.

Test yourself

Which condition finds orders without a shipped date?

  1. shipped_date = NULL
  2. shipped_date = ''
  3. shipped_date IS NULL
  4. shipped_date == 0

What is COUNT(email) when 4 of 25 customers have a NULL email?

  1. 25
  2. 21
  3. 4
  4. NULL

What does 10 + NULL evaluate to?

  1. 10
  2. 0
  3. NULL
  4. An error