Skip to main content

How to Find Missing Values in SQLite with IS NULL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

To find contacts without a phone number, it seems natural to write phone = NULL, but that finds nothing. Missing values need IS NULL. This guide finds the contacts with and without a phone, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT <columns> FROM <table> WHERE <column> IS NULL;
SELECT <columns> FROM <table> WHERE <column> IS NOT NULL;
PartWhat it means
<column> IS NULLTrue for the rows where the column has no value
<column> IS NOT NULLTrue for the rows where the column has a value

SQLite's documentation on IS and IS NOT: "The IS and IS NOT operators work like = and != except when one or both of the operands are NULL."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as is_null.sql:

is_null.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, phone) VALUES ('Cleo', '555-0103');

SELECT name, phone FROM contacts WHERE phone IS NULL;
SELECT name, phone FROM contacts WHERE phone IS NOT NULL;

The is_null.sql script in a macOS-style code window: a contacts table, inserts of Ana and Cleo with phones and Ben with NULL, a SELECT of the contacts whose phone IS NULL, and one of those whose phone IS NOT NULL.

  • Ben has no phone. Ana and Cleo do.
  • WHERE phone IS NULL keeps the rows with no phone.
  • WHERE phone IS NOT NULL keeps the rows that have one.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table contacts.db ".read is_null.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.

Output
+------+-------+
| name | phone |
+------+-------+
| Ben | |
+------+-------+
+------+----------+
| name | phone |
+------+----------+
| Ana | 555-0101 |
| Cleo | 555-0103 |
+------+----------+

The output in a terminal: the first result holds Ben with an empty phone, and the second holds Ana with 555-0101 and Cleo with 555-0103.

IS NULL found Ben, and IS NOT NULL found Ana and Cleo. Every contact landed in exactly one of the two results.

Common Mistakes​

MistakeWhat happensDo this instead
WHERE phone = NULLNo rows, not even Ben: = with a NULL gives NULL, which is never trueWHERE phone IS NULL
WHERE phone != '555-0101' to find everyone but AnaOnly Cleo came back: Ben's missing phone isn't "not equal" eitherWHERE phone IS NOT '555-0101' returned Ben and Cleo

Troubleshooting​

SymptomLikely causeFix
A WHERE with = NULL returns nothing= never matches a missing valueUse IS NULL
Parse error near line 1 of is_null.sql: table contacts already existsThe script already ran on contacts.dbDelete contacts.db and run the script again

FAQ​

Why doesn't = NULL work?​

NULL means "no value", so SQLite can't say whether it equals anything: 'x' = NULL and even NULL = NULL both gave NULL. A WHERE keeps only the rows where its condition is true. IS always gives 1 or 0. The documentation: "It is not possible for an IS or IS NOT expression to evaluate to NULL."

Can I see the result of IS NULL for every row?​

Yes, as a column: SELECT name, phone IS NULL FROM contacts; returned 0 for Ana, 1 for Ben and 0 for Cleo. 1 means true.

Conclusion​

WHERE phone IS NULL found Ben, the one contact without a phone, and IS NOT NULL found the two with one, where = NULL would have found nobody.

Additional Resources​