SQL NULL Values Get Certified
Learn what NULL means in SQL and how to work with missing, unknown, or unavailable values in SQLite.
What is a NULL Value?
In SQL, NULL represents a missing, unknown, or unavailable value. It is not the same as zero, an empty string, or a value containing spaces.
SQLite supports NULL values and provides special operators such as
IS NULL and IS NOT NULL for working with them.
Inserting a NULL Value
You can explicitly insert NULL into a column when a value is not available.
What this code does: Creates a devices table and inserts a Tablet whose serial number is not known yet, so the statement writes NULL (without quotes) in the serial_number column. The final SELECT shows the row with an empty serial number, which means “no value” rather than any real text.
CREATE TABLE devices (
id INTEGER,
name TEXT,
serial_number TEXT
);
INSERT INTO devices (id, name, serial_number)
VALUES (1, 'Tablet', NULL);
SELECT * FROM devices;
NULL is Not Zero
A value of 0 is an actual numeric value. NULL means that no value is available.
What this code does: Stores two items in an inventory table. The Notebook has a quantity of 0, meaning we know there are none in stock. The Marker has NULL, meaning its quantity has not been recorded at all. The result displays both rows so you can see that zero and NULL are different things.
CREATE TABLE inventory (
item TEXT,
quantity INTEGER
);
INSERT INTO inventory (item, quantity)
VALUES
('Notebook', 0),
('Marker', NULL);
SELECT * FROM inventory;
Finding Non-NULL Values
Use IS NOT NULL to return rows where a column contains an actual value.
What this code does: Builds a courses table where the Geology course has no instructor yet. Filtering with WHERE instructor IS NOT NULL keeps only the courses that already have an instructor, so Astronomy (Dr. Miller) and Robotics (Dr. Thompson) are returned and Geology is skipped.
CREATE TABLE courses (
id INTEGER,
title TEXT,
instructor TEXT
);
INSERT INTO courses (id, title, instructor)
VALUES
(1, 'Astronomy', 'Dr. Miller'),
(2, 'Geology', NULL),
(3, 'Robotics', 'Dr. Thompson');
SELECT * FROM courses
WHERE instructor IS NOT NULL;
NULL with WHERE
NULL values require special handling when filtering records with a WHERE clause.
What this code does: Stores three shipments, one of which has no tracking code yet. The query selects only the product and tracking_code columns and uses IS NULL in the WHERE clause to find shipments that still need a tracking code. Only the Mouse shipment is returned.
CREATE TABLE shipments (
id INTEGER,
product TEXT,
tracking_code TEXT
);
INSERT INTO shipments (id, product, tracking_code)
VALUES
(101, 'Keyboard', 'TRK1001'),
(102, 'Mouse', NULL),
(103, 'Monitor', 'TRK1003');
SELECT product, tracking_code
FROM shipments
WHERE tracking_code IS NULL;
NULL is Different from an Empty String
An empty string '' is a text value containing no characters. It is different from NULL.
What this code does: Inserts two profiles: ‘skywalker’ has an empty string '' as a nickname, which is a real text value with zero characters, while ‘starlight’ has NULL, meaning no nickname was ever set. Both nicknames look blank in the result, but they are stored differently, and a query using IS NULL would match only starlight.
CREATE TABLE profiles (
id INTEGER,
username TEXT,
nickname TEXT
);
INSERT INTO profiles (id, username, nickname)
VALUES
(1, 'skywalker', ''),
(2, 'starlight', NULL);
SELECT
id,
username,
nickname
FROM profiles;
Replacing NULL with IFNULL()
SQLite provides the IFNULL() function to replace a NULL value with another value.
What this code does: Lists each member’s name and phone number, but wraps the phone column in IFNULL(phone, 'Not Available'). If the phone has a value, it is shown as normal; if it is NULL, the text ‘Not Available’ is shown instead. Michael and William keep their numbers, while Emily’s missing phone appears as ‘Not Available’.
CREATE TABLE members (
id INTEGER,
name TEXT,
phone TEXT
);
INSERT INTO members (id, name, phone)
VALUES
(1, 'Michael', '5551234567'),
(2, 'Emily', NULL),
(3, 'William', '5559876543');
SELECT
name,
IFNULL(phone, 'Not Available') AS phone
FROM members;
Using COALESCE() with NULL
SQLite also supports COALESCE(), which returns the first non-NULL value from the supplied expressions.
What this code does: For each contact, COALESCE(mobile, email, 'No Contact') checks the mobile number first, then the email, and finally falls back to the text ‘No Contact’. Olivia has no mobile, so her email is used. James has a mobile number, so that is shown. Ava has neither, so she gets ‘No Contact’.
CREATE TABLE contacts (
id INTEGER,
name TEXT,
mobile TEXT,
email TEXT
);
INSERT INTO contacts (id, name, mobile, email)
VALUES
(1, 'Olivia', NULL, 'olivia@example.com'),
(2, 'James', '5550011111', NULL),
(3, 'Ava', NULL, NULL);
SELECT
name,
COALESCE(mobile, email, 'No Contact') AS contact
FROM contacts;
Counting NULL and Non-NULL Values
COUNT(column) counts only non-NULL values, while COUNT(*) counts all rows.
What this code does: Compares two ways of counting in one query. COUNT(*) counts every row in the events table, giving 4 total events. COUNT(venue) skips rows where the venue is NULL, so it counts only the 2 events that already have a venue (Tech Expo and Book Festival).
CREATE TABLE events (
id INTEGER,
event_name TEXT,
venue TEXT
);
INSERT INTO events (id, event_name, venue)
VALUES
(1, 'Tech Expo', 'Hall A'),
(2, 'Science Fair', NULL),
(3, 'Book Festival', 'Hall C'),
(4, 'Art Show', NULL);
SELECT
COUNT(*) AS total_events,
COUNT(venue) AS events_with_venue
FROM events;
Sorting NULL Values
NULL values can also appear when sorting query results with ORDER BY.
What this code does: Sorts the tasks by priority from lowest to highest. In SQLite, NULL is treated as smaller than any real value, so ‘Update Website’ (priority NULL) appears first, followed by Check Database (1), Backup Files (2) and Write Report (3). Other databases may place NULL values last, so this ordering is worth remembering.
CREATE TABLE tasks (
id INTEGER,
task_name TEXT,
priority INTEGER
);
INSERT INTO tasks (id, task_name, priority)
VALUES
(1, 'Backup Files', 2),
(2, 'Update Website', NULL),
(3, 'Check Database', 1),
(4, 'Write Report', 3);
SELECT *
FROM tasks
ORDER BY priority;
Handling NULL in Calculations
You can use IFNULL() to treat a NULL numeric value as another number before performing a calculation.
What this code does: Fixes the problem from the previous example. IFNULL(tax, 0) turns a missing tax into 0 before it is added to the amount. Printer totals 13200 and Projector totals 27500, and Scanner, which has no tax recorded, now correctly totals 8000 instead of NULL.
CREATE TABLE bills (
item TEXT,
amount REAL,
tax REAL
);
INSERT INTO bills (item, amount, tax)
VALUES
('Printer', 12000, 1200),
('Scanner', 8000, NULL),
('Projector', 25000, 2500);
SELECT
item,
amount,
tax,
amount + IFNULL(tax, 0) AS total
FROM bills;
NULL with NOT
When checking for missing values, use IS NOT NULL rather than trying to compare a column with NULL.
What this code does: Lists the instructors who have been assigned a room. WHERE room IS NOT NULL keeps Dr. Anderson (R101) and Dr. Wilson (R203), and removes Dr. Martinez, whose room is NULL. Writing room != NULL instead would return no rows at all, which is a common beginner mistake.
CREATE TABLE instructors (
id INTEGER,
name TEXT,
room TEXT
);
INSERT INTO instructors (id, name, room)
VALUES
(1, 'Dr. Anderson', 'R101'),
(2, 'Dr. Martinez', NULL),
(3, 'Dr. Wilson', 'R203');
SELECT *
FROM instructors
WHERE room IS NOT NULL;
NULL Values with CASE
A CASE expression can be used to display a meaningful message when a column contains NULL.
What this code does: Reads each applicant’s result and uses CASE to show a friendlier status. When result IS NULL, the output is ‘Pending’; otherwise the stored result is displayed. Ethan shows ‘Selected’, Noah shows ‘Rejected’, and Isabella, who has no result yet, shows ‘Pending’.
CREATE TABLE applications (
applicant TEXT,
result TEXT
);
INSERT INTO applications (applicant, result)
VALUES
('Ethan', 'Selected'),
('Isabella', NULL),
('Noah', 'Rejected');
SELECT
applicant,
CASE
WHEN result IS NULL THEN 'Pending'
ELSE result
END AS application_status
FROM applications;
Common NULL Operations
WHERE phone IS NULLWHERE phone IS NOT NULLIFNULL(phone, 'Unknown')COALESCE(phone, email)COUNT(phone)NULL vs Other Values
NULLphone = NULL0quantity = 0''nickname = '''NULL'status = 'NULL'Advantages of Understanding NULL
- Helps represent missing or unavailable information.
- Makes database records more flexible.
- Allows incomplete data to be handled correctly.
- Helps produce accurate filtering and reporting queries.
- Functions such as
IFNULL()andCOALESCE()make NULL easier to handle.
Best Practices
- Use
IS NULLinstead of= NULL. - Use
IS NOT NULLwhen searching for available values. - Do not confuse NULL with zero or an empty string.
- Use
IFNULL()orCOALESCE()when a fallback value is needed. - Use
NOT NULLconstraints when a column must always contain a value. - Consider how NULL values affect calculations and aggregate functions.
Understanding NULL values is essential for working with incomplete or unavailable data in SQL. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.