r/learnSQL • u/thequerylab • 22m 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.
| Friend | City | Trip Day | Total Spent |
|---|---|---|---|
| Harry | New York | 1 | $200 |
| Johny | New York | 2 | $190 |
| Nicholas | Las Vegas | 1 | $190 |
| Bailey | Las Vegas | 2 | $180 |
| Harry | Las Vegas | 3 | $190 |
| Emma | NULL | 4 | $150 |
| Sam | New York | 5 | NULL |
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 | $150Sam 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; -- 7Now you count how many people logged their spend.
SELECT COUNT(spent) FROM expenses; --6COUNT(*) 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 insteadRemember: COALESCE replaces NULL with a value you choose.
The full NULL cheat sheet:
| Situation | What happens | Fix |
|---|---|---|
| WHERE col = NULL | No rows returned | Use IS NULL |
| WHERE col != NULL | No rows returned | Use IS NOT NULL |
| NULL in comparisons | Row disappears | Use COALESCE |
| COUNT(column) | Skips NULL rows | Use COUNT(*) if you want all rows |
| SUM / AVG | Ignores NULL | Wrap in COALESCE(col, 0) |
| NULL in JOIN | Row disappears | Use 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