r/learnSQL • • 28m ago

Before your next SQL interview, keep this post handy - NULL: The Silent Bug (Part 6)

Simple, practical stuff that sticks in your head when you're actually in the interview.

AI can help at work, but in an interview, your brain still has to do the job!!!

Same trip. Two new friends joined.

FriendCityTrip DayTotal Spent
HarryNew York1$200
JohnyNew York2$190
NicholasLas Vegas1$190
BaileyLas Vegas2$180
HarryLas Vegas3$190
EmmaNULL4$150
SamNew York5NULL

Emma came on the trip but forgot to write which city she was in.

Sam came but forgot to log what he spent.

That empty gap in the data? That is NULL.

NULL does not mean zero. NULL does not mean empty. NULL means we do not know.

And not knowing quietly breaks SQL in ways that give no error.

1. NULL = NULL is FALSE

You want to find Emma. Her city is NULL. You try this:

SELECT * FROM expenses WHERE city = NULL;

No data will be returned. No error as well.

Why? Because NULL means unknown. SQL cannot say unknown = unknown is true. It does not know.

The right way:

SELECT * FROM expenses WHERE city IS NULL;

Remember: Never use = NULL. Always use IS NULL.

2. NULL in comparisons always loses

You want everyone who spent less than $200.

SELECT * FROM expenses WHERE spent < 200

Johny    | New York  | $190
Nicholas | Las Vegas | $190
Bailey   | Las Vegas | $180
Harry    | Las Vegas | $190
Emma     | NULL      | $150

Sam is gone. His spend is NULL.

Sam did not spend $200. Sam did not spend $0. We just do not know what Sam spent.

SQL does not include what it does not know.

Remember: Any comparison with NULL gives unknown. That row disappears silently.

3. COUNT(*) vs COUNT(spent)

You want to count how many people are on the trip.

SELECT COUNT(*) FROM expenses; -- 7

Now you count how many people logged their spend.

SELECT COUNT(spent) FROM expenses; --6

COUNT(*) counts every row. COUNT(spent) skips NULL.

Sam's NULL spend is invisible to COUNT(spent). No warning. No error.

Remember: COUNT(*) counts rows. COUNT(column) skips NULL. They can give different numbers.

4. SUM and AVG silently ignore NULL

You want the average spend per person.

SELECT AVG(spent) FROM expenses;

SQL adds up 6 known values and divides by 6. Sam is not counted at all.

The average is not across 7 people. It is across 6.

If Sam spent $0, the average would be lower. But we do not know. So SQL skips him.

Remember*: SUM and AVG ignore NULL rows. Your totals and averages may be missing data.*

5. COALESCE - replace NULL with something useful

You want to show expenses but replace NULL city with "Unknown".

SELECT name, COALESCE(city, 'Unknown') AS city, spent FROM expenses;

Emma now shows "Unknown" instead of NULL.

COALESCE takes the first value that is not NULL. You can give it a backup.

COALESCE(spent, 0)   
-- if spent is NULL, show 0 instead

Remember: COALESCE replaces NULL with a value you choose.

The full NULL cheat sheet:

SituationWhat happensFix
WHERE col = NULLNo rows returnedUse IS NULL
WHERE col != NULLNo rows returnedUse IS NOT NULL
NULL in comparisonsRow disappearsUse COALESCE
COUNT(column)Skips NULL rowsUse COUNT(*) if you want all rows
SUM / AVGIgnores NULLWrap in COALESCE(col, 0)
NULL in JOINRow disappearsUse LEFT JOIN + IS NULL check

If you want more posts like this, let me know in the comments and I'll keep the series going!

New to SQL and want hands-on practice with real data? I built SQL Zero to Confident - completely free. TheQueryLab

2 Upvotes

1 comment sorted by

1

u/cwjinc 12m ago

My favorite silent bug: Implicit type conversions in a where/join clause.
They can wait years until a parser/optimizer change reverses the side that is converted and breaks the query.