NULL, COUNT and Division Traps in SQL Interviews

SQL Interview Problems: 12 Classic Questions Solved · Counting, Traps and Speed · 5 min read · Updated

The NULL traps

  1. A NULL means the value is unknown. It is not zero and not an empty text.
  2. Compare an unknown value with anything, and you cannot say yes or no.
  3. The result of the comparison is unknown, not true and not false.
  4. WHERE keeps only rows where the condition is true, so these rows disappear without any warning.
  5. Test with IS NULL, or replace the NULL with COALESCE when a default makes sense.

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

  1. Could any column in my filter, join or calculation be NULL?
  2. Did I use IS NULL and not = NULL?
  3. Does my NOT IN list contain NULL?
  4. Which COUNT do I need: rows, values or different values?
  5. Am I dividing two whole numbers?
  6. Do the parts add up to the total?

Your task

  1. Count the employees whose department id is NULL. Is the answer what you expected?
  2. Calculate the average salary of the Finance department in two ways: ignoring the missing salary and counting it as zero.
  3. Write the percent of employees who are paid 100000 or more, in a way that gives 42.9 percent and not 0.
  4. Find out what SELECT 1 = NULL returns, and what SELECT NULL IS NULL returns.

How to check it worked

  • salary = NULL returns 0 rows and salary IS NULL returns Jack Reed.
  • The not-equal query returns 11, and 12 after adding the NULL test.
  • Your percent query uses 100.0 or a CAST and returns about 42.9.

Frequently asked questions

What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts all rows. COUNT(column) counts only rows where that column is not NULL.

Does AVG count NULL values as zero?

No. AVG ignores NULL values, so it divides by the number of non-NULL values. Use COALESCE inside AVG if you want NULL to count as zero.

Why does 7 / 2 give 3 in some databases?

When both numbers are whole numbers, many databases return a whole number. Write 7 / 2.0 or cast one side to a decimal to get 3.5.