☰ Learn SQL Tutorial Menu

Understanding NULL

Written by Faisal Adnan · Updated 14 Oct 2026 · 3 min read


NULL represents missing or unknown data — it is not zero, not an empty string, and not "false." It simply means "we don't have a value here."

Why NULL breaks normal logic

In SQL, comparing anything to NULL with = or <> doesn't return true or false — it returns unknown, which is treated as "not matched." That's why this query returns nothing, even for rows where manager_id is genuinely empty:

-- Wrong — this will never match anything
SELECT * FROM employees WHERE manager_id = NULL;

Always use IS NULL / IS NOT NULL instead:

SELECT * FROM employees WHERE manager_id IS NULL;

Result: first 5 of 6 rows (the order can differ on other engines unless you add ORDER BY)

idfirst_namelast_nameemaildepartment_idjob_titlesalaryhire_datemanager_id
1AliSheikhali.sheikh1@csa-demo.local1Engineering Manager1880002024-10-16NULL
2SaraQureshisara.qureshi2@csa-demo.local2Business Development Executive1490002019-09-22NULL
3AhmedQureshiahmed.qureshi3@csa-demo.local3SEO Specialist1010002023-12-28NULL
4AyeshaSheikhayesha.sheikh4@csa-demo.local4HR Manager770002019-08-24NULL
5BilalAhmedbilal.ahmed5@csa-demo.local5Finance Manager1220002020-06-19NULL

NULL in calculations

Any arithmetic involving NULL produces NULL. If a column allowed NULL prices, price * quantity would be NULL for those rows.

Replacing NULL with a default value

Use COALESCE (works on every engine) to substitute a fallback value:

SELECT first_name,
       COALESCE(manager_id, 0) AS manager_id_or_zero
FROM employees;

Result: first 5 of 26 rows (the order can differ on other engines unless you add ORDER BY)

first_namemanager_id_or_zero
Ali0
Sara0
Ahmed0
Ayesha0
Bilal0

COALESCE returns the first non-null value from its argument list — you can pass more than two.

NULL and aggregate functions

Functions like COUNT, SUM, and AVG silently ignore NULL values rather than treating them as zero — an important detail we'll revisit in the aggregate functions article.

Open the Code Playground, pick any SQL engine (SQLite, PostgreSQL, MySQL, or SQL Server), and paste these queries in directly — the practice database above is already loaded for you, every time.

Lesson 5 of 27

Sign in to track your progress and earn XP for every lesson you finish.

Practice tip: the built-in code runner is being upgraded. Until it is back, copy the examples into any SQL or Python tool you have (for example SQLite, PostgreSQL, MySQL, SQL Server or a free online editor) and run them there. You can keep reading and earning XP here.