A big share of wrong SQL answers come from five small facts about NULL and numbers. They are favourite interview questions because the wrong queries still run and still return something that looks plausible. This lesson collects them in one place so you can test yourself against each one. In SQL, NULL does not mean zero or empty text. It means unknown.
Trap 1: = NULL never matches
Because NULL is unknown, asking whether an unknown value equals something gives unknown, and WHERE keeps only rows where the condition is true. So a comparison with = NULL finds nothing, even when NULL values exist.
SELECT COUNT(*) AS rows_found
FROM employees
WHERE salary = NULL;
rows_found
0
The correct test is IS NULL, or IS NOT NULL:
SELECT name
FROM employees
WHERE salary IS NULL;
name
Jack Reed
Trap 2: not-equal silently drops NULL rows
How many employees do not earn 120000? Fourteen people, two earn 120000, so you would expect 12.
SELECT COUNT(*) AS not_120k
FROM employees
WHERE salary <> 120000;
not_120k
11
The answer is 11. Jack Reed has no salary, and NULL <> 120000 is unknown, so he is not counted. If the question means "everyone except the people who earn exactly 120000", add the NULL test:
SELECT COUNT(*) AS not_120k
FROM employees
WHERE salary <> 120000 OR salary IS NULL;
not_120k
12
Now 12. Whether Jack should count is a question for the interviewer, which is exactly the clarifying question from lesson 1.
Trap 3: three kinds of COUNT
COUNT(*)counts rows, whatever they contain.COUNT(column)counts the rows where that column is not NULL.COUNT(DISTINCT column)counts the different non-NULL values.
The same table gives different answers, and they are all correct for their own question:
SELECT COUNT(*) AS all_rows,
COUNT(salary) AS with_salary,
COUNT(DISTINCT dept_id) AS departments_used,
ROUND(AVG(salary)) AS avg_ignoring_null,
ROUND(SUM(salary) * 1.0 / COUNT(*)) AS avg_if_null_counted
FROM employees;
all_rows | with_salary | departments_used | avg_ignoring_null | avg_if_null_counted
14 | 13 | 4 | 95923 | 89071
There are 14 rows, 13 salaries, 4 different department ids (Legal has nobody), and two different averages. AVG(salary) ignores NULL, so it divides by 13 and gives about 95923. If you count the missing salary as zero, you divide by 14 and get about 89071. Neither is wrong. They answer different questions, and you must say which one you mean. When the missing value is a real zero, such as no commission earned, use COALESCE(salary, 0). When it is unknown, as for a new hire, leaving it out is more honest.
Trap 4: NOT IN with NULL
You met this in lesson 5. A single NULL in the list of a NOT IN subquery makes the whole condition unknown, and the query returns no rows. Prefer NOT EXISTS, or filter out the NULL values inside the subquery.
Trap 5: whole-number division
In many databases, dividing two whole numbers gives a whole number. A percentage calculated as 5 / 20 * 100 may return 0, because 5 / 20 is already rounded down to 0.
SELECT 7 / 2 AS plain,
7 / 2.0 AS with_decimal,
CAST(7 AS REAL) / 2 AS with_cast;
plain | with_decimal | with_cast
3 | 3.5 | 3.5
Here 7 / 2 gives 3, not 3.5, in SQLite, as it does in PostgreSQL and SQL Server. MySQL is different and returns 3.5 for /, and uses DIV for whole-number division. The safe habit is to make one side a decimal on purpose, with 7 / 2.0, 7 * 1.0 / 2 or a CAST. And protect a division by a column that could be zero with NULLIF(column, 0), as in lesson 8.
A quick checklist before you submit
- Could any column in my filter, join or calculation be NULL?
- Did I use
IS NULLand not= NULL? - Does my
NOT INlist contain NULL? - Which
COUNTdo I need: rows, values or different values? - Am I dividing two whole numbers?
- Do the parts add up to the total?
Your task
- Count the employees whose department id is NULL. Is the answer what you expected?
- Calculate the average salary of the Finance department in two ways: ignoring the missing salary and counting it as zero.
- Write the percent of employees who are paid 100000 or more, in a way that gives 42.9 percent and not 0.
- Find out what
SELECT 1 = NULLreturns, and whatSELECT NULL IS NULLreturns.
How to check it worked
salary = NULLreturns 0 rows andsalary IS NULLreturns Jack Reed.- The not-equal query returns 11, and 12 after adding the NULL test.
- Your percent query uses
100.0or a CAST and returns about 42.9.