NULL in SQL: 5 Common Mistakes and Fixes
NULL means unknown, not zero. Learn the five SQL NULL mistakes beginners make, from = NULL to NOT IN, and the exact fix for each, with a table you can run yourself.
Short answer: NULL in SQL means “no value” or “unknown”, and it is not the same as zero or an empty string. Because NULL is unknown, comparisons like = NULL never match anything, and arithmetic with NULL gives NULL. Below are the five mistakes beginners make most often, each with a fix you can copy.
What Is NULL in SQL?
A NULL marks a missing or unknown value. A student whose city was never entered has a NULL city; that is different from a student whose score is 0 (a known score) or whose name is '' (a known, empty text). SQL uses three-valued logic: a condition can be true, false, or unknown. A WHERE clause keeps only the rows where the condition is true, so unknown rows quietly disappear.
All examples use this small table. You can run them in SQLite, or in any SQL database with minor changes. If you are new to SQL, start with what SQL is used for.
import sqlite3
c = sqlite3.connect(":memory:")
c.executescript('''
CREATE TABLE students(id INTEGER, name TEXT, city TEXT, score INTEGER);
INSERT INTO students VALUES
(1, 'Asha', 'Rivermouth', 80),
(2, 'Ben', NULL, NULL),
(3, 'Chen', 'Hillford', 60),
(4, 'Dev', NULL, 90);
CREATE TABLE banned(id INTEGER);
INSERT INTO banned VALUES (2), (NULL);
''')
def show(sql):
print(sql)
print(" ->", c.execute(sql).fetchall())
show("SELECT name FROM students WHERE city = NULL")
show("SELECT name FROM students WHERE city IS NULL")
Mistake 1: Testing for NULL with = instead of IS NULL
city = NULL asks “is city equal to an unknown?”, and the answer is unknown, never true. It returns no rows. Use IS NULL or IS NOT NULL.
SELECT name FROM students WHERE city IS NULL; -- Ben, Dev
SELECT name FROM students WHERE city IS NOT NULL; -- Asha, ChenMistake 2: Forgetting that NULL spreads through calculations
score + 10 is NULL when score is NULL, so Ben's bonus disappears instead of becoming 10. If a missing value should count as a default, say so with COALESCE, which returns its first non-NULL argument.
SELECT name, score + 10 AS bonus FROM students;
-- Ben's bonus is NULL
SELECT name, COALESCE(score, 0) + 10 AS bonus FROM students;
-- Ben's bonus is 10Only use a default when it is true to the data. A missing score is not really a score of zero, which leads to the next mistake.
Mistake 3: Misreading COUNT and AVG
COUNT(*) counts rows; COUNT(score) counts only rows where score is not NULL. Aggregates such as AVG, SUM, MIN and MAX skip NULLs too.
SELECT COUNT(*), COUNT(score), AVG(score) FROM students;
-- 4, 3, 76.666... (average of 80, 60, 90)
SELECT AVG(COALESCE(score, 0)) FROM students;
-- 57.5 (Ben counted as 0)Neither answer is automatically right. The first averages the students who took the test; the second treats a missing score as a zero. Decide which question you are really asking. The tutorial on data types, NULL and choosing the right type goes deeper.
Mistake 4: Expecting not-equal to include NULL rows
Ask for students whose city is not Hillford and you might expect Ben and Dev to appear. They do not, because NULL <> 'Hillford' is unknown.
SELECT name FROM students WHERE city <> 'Hillford';
-- Asha only
SELECT name FROM students WHERE city <> 'Hillford' OR city IS NULL;
-- Asha, Ben, DevWhenever a column can be NULL, ask what should happen to those rows and handle them on purpose. See BETWEEN, IN, LIKE and NULL for related filters.
Mistake 5: Using NOT IN with a list that contains NULL
This is the sneakiest one. If the subquery returns even one NULL, NOT IN returns no rows at all, because every comparison against that NULL is unknown.
SELECT name FROM students
WHERE id NOT IN (SELECT id FROM banned);
-- no rows at all, because banned contains a NULL
SELECT name FROM students s
WHERE NOT EXISTS (SELECT 1 FROM banned b WHERE b.id = s.id);
-- Asha, Chen, DevPrefer NOT EXISTS, or add WHERE id IS NOT NULL inside the subquery.
How Do You Avoid NULL Problems?
Mark columns
NOT NULLwhen a value is truly required.Use
IS NULL,COALESCEandNOT EXISTSdeliberately.Keep NULL (unknown) separate from
0and''(known values).Check a few NULL rows by eye after each new query.
Interview Tip
A favourite question is “why does WHERE x = NULL return nothing?” Answer in one line: NULL means unknown, so the comparison is unknown rather than true, and you test it with IS NULL.
Key Takeaway
NULL is unknown, not zero. Test it with IS NULL, handle it with COALESCE, remember that aggregates skip it, and avoid NOT IN over lists that may contain it.
Ready to start? Practise filtering, grouping and handling missing data with hands-on SQL lessons and quizzes. Begin with the free course SQL and Data Handling for AI: Beginner.
Related articles
More in SQL and Data
SQL Window Functions Explained: ROW_NUMBER, RANK and More
SQL window functions add a calculated value to every row without collapsing rows like GROUP BY. See ROW_NUMBER, RANK and DENSE_RANK side by side with real result rows.
6 min readNov 28, 2026SQL vs Excel: When to Use Each
A clear comparison of SQL and Excel: what each is good at, the same task done in both, and how to decide which to use or how to combine them.
4 min readNov 24, 2026SQL JOINs Explained With Examples
INNER, LEFT, RIGHT and FULL JOIN explained with two tiny tables, the exact results of each query, how to find unmatched rows, and the mistakes that trip people up.
4 min readNov 18, 2026SQL vs Python for Data Analysis
SQL and Python do different jobs in data analysis. See what each is best at, a side-by-side example, and which one to learn first.
4 min readNov 15, 2026Primary Key vs Foreign Key in SQL
What primary and foreign keys are, how they differ, and how a runnable customers-and-orders example shows the database enforcing them.
4 min readNov 13, 2026WHERE vs GROUP BY vs HAVING in SQL
WHERE filters rows, GROUP BY builds groups and HAVING filters groups. See one worked example, the order SQL runs them in, and the mistakes to avoid.
3 min readNov 10, 2026
Comments
Sign in to join the discussion.

