TGViewer
Coding Interview Preparation Coding Interview Preparation @coding_interview_preparation · 5.9K subscribers
Post #1425 306
📊 SQL TUESDAY #7 (BONUS) - NULLs: The Silent Query Killer

NULL doesn't behave like a normal value, and it quietly breaks queries that "look" correct. This trips up even experienced engineers.


customers
+----+---------+---------+
| id | name | phone |
+----+---------+---------+
| 1 | Alice | 555-1234|
| 2 | Bob | NULL |
| 3 | Charlie | NULL |


⚠️ Trap #1: WHERE phone = NULL returns ZERO rows - always. NULL means "unknown," and "is unknown equal to unknown?" is itself unknown, not true. You must use IS NULL:

sql
SELECT * FROM customers WHERE phone IS NULL; -- ✅ correct


⚠️ Trap #2: COUNT(phone) vs COUNT(*) give different results. COUNT(*) counts all rows; COUNT(column) only counts non-NULL values in that column.

sql
SELECT COUNT(*) FROM customers; -- 3
SELECT COUNT(phone) FROM customers; -- 1


⚠️ Trap #3: NULL values are often silently EXCLUDED from aggregate calculations in ways people don't expect:

sql
SELECT AVG(phone_call_count) FROM customers;
-- NULLs are ignored entirely, NOT treated as 0.
-- If you wanted them treated as 0, use:
SELECT AVG(COALESCE(phone_call_count, 0)) FROM customers;


COALESCE(value, default) returns the first non-NULL argument - extremely useful for handling missing data gracefully instead of letting it silently skew your results.

Has a NULL-related bug ever quietly thrown off a real report at your job? 👇
More from @coding_interview_preparation
  1. Oct 8, 2026If you're prepping for system design interviews, this repo is gold It contains a curated,…
  2. Oct 6, 2026document post
  3. Oct 4, 2026💼 Why Your Resume Gets Rejected Before a Human Reads It You may have good skills and proj…
  4. Oct 2, 2026🧠 Coding Myths You Should Stop Believing There's a lot of advice online about learning to…
  5. Oct 1, 2026Most Asked Topics in AI Engineer Interviews Based on 2026 candidate reports
  6. Sep 30, 2026💼 What Companies Actually Look For in a Fresher Think companies only care about your CGPA…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →