How to Store a Missing Value in SQLite with NULL
Not every value is known: a contact may have no phone number. SQLite stores a missing value as NULL, which isn't zero or empty text but no value at all. This guide stores two contacts without a phone, with real output from SQLite 3.54.1.
Syntax
INSERT INTO <table> (<column 1>, <column 2>) VALUES (<value>, NULL);
| Part | What it means |
|---|---|
NULL | The missing value, written without quotes |
A column left out of the INSERT | Also gets NULL, when it has no default value |
typeof(<column>) | Returns the type of each value: null for a missing one |
SQLite's documentation on typeof(): "The typeof(X) function returns a string that indicates the datatype of the expression X: "null", "integer", "real", "text", or "blob"."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as null_value.sql:
CREATE TABLE contacts (
name TEXT NOT NULL,
phone TEXT
);
INSERT INTO contacts (name, phone) VALUES ('Ana', '555-0101');
INSERT INTO contacts (name, phone) VALUES ('Ben', NULL);
INSERT INTO contacts (name) VALUES ('Cleo');
SELECT name, phone, typeof(phone) FROM contacts;

- Ana has a phone number.
- Ben's phone is written as
NULL, without quotes. - Cleo's
INSERTleaves the phone out. typeof(phone)shows the type of each stored phone.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table contacts.db ".read null_value.sql"
sqlite3 opens the database file contacts.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+------+----------+---------------+
| name | phone | typeof(phone) |
+------+----------+---------------+
| Ana | 555-0101 | text |
| Ben | | null |
| Cleo | | null |
+------+----------+---------------+

Ben's and Cleo's phones are empty cells, and typeof says null for both: writing NULL and leaving the column out stored the same thing. Ana's phone is text.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing 'NULL' in quotes | The four letters NULL were stored as text, and typeof said text | Write NULL without quotes |
| Calculating with a missing value | NULL + 1 and 'Ben' || NULL both gave NULL | Swap in a value first: coalesce(NULL, 0) + 1 gave 1 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A cell shows nothing | The value is NULL, which -table prints as an empty cell | Run with -nullvalue NULL to print the word NULL instead |
count(phone) is smaller than count(*) | count(<column>) skips NULL values | Use count(*) to count rows |
Parse error near line 1 of null_value.sql: table contacts already exists | The script already ran on contacts.db | Delete contacts.db and run the script again |
FAQ
How do I tell a missing value from empty text?
With typeof(): it returned null for NULL and text for ''. In the shell, -nullvalue NULL also helps: sqlite3 -table -nullvalue NULL contacts.db prints NULL in every cell that has no value.
Does count() count missing values?
count(*) counts rows, and count(phone) counts only the phones that are there. On this table they returned 3 and 1.
What does coalesce() do?
It returns its first argument that isn't NULL. The documentation: "The coalesce() function returns a copy of its first non-NULL argument, or NULL if all arguments are NULL."
Conclusion
NULL, written without quotes or by leaving the column out, stored Ben's and Cleo's missing phones, and typeof(phone) showed null for both.
Additional Resources
- Datatypes In SQLite —
NULLand the other storage classes. - Core functions —
typeof(),coalesce()and more.
