Skip to content

Null Hunt

You'll find out why NULL is not zero, why COUNT(*) and COUNT(column) disagree, and why your dates sort wrong, on a real file that has all three problems.

About

Nearly every quietly wrong answer in SQL comes from one of three places: a NULL that got treated like a value, a number stored as text, or a date in a format that doesn't sort. Nobody tells beginners this, so they trust a total that's missing a third of the rows. This badge is an evening with a messy file, finding each of those problems and fixing it in the query or in the table. You'll want a CSV with blanks, an 'N/A' in a number column and dates like 3/4/2024, and most bank, fitness and government exports oblige. What counts is each trap found and shown on your data, not a clean file where nothing goes wrong.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
6
Takes
One evening
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Load a messy CSV and count the rows where a column is NULL, blank, and the text 'N/A', separately. Three queries: WHERE col IS NULL, WHERE col = '', and WHERE col = 'N/A'. The first one catches almost everyone, because col = NULL is never true, and only IS NULL works. Those three counts are three different kinds of missing, and a report that treats them as one is already wrong. Take a screenshot of the three queries and counts. Photo
  2. 2
    Show COUNT(*), COUNT(amount) and AVG(amount) on the same table and explain the gap. COUNT(*) counts rows, COUNT(amount) counts rows where amount isn't NULL, and AVG ignores NULLs, so the average of a column with blanks is the average of the rows that bothered to fill it in. Write one sentence in your post on which of those you actually wanted. Take a screenshot of the three numbers side by side. Photo
  3. 3
    Find a number column that imported as text and fix it with CAST or a new column. In SQLite, SELECT typeof(amount), COUNT(*) FROM t GROUP BY 1 shows you the mix. If some rows say text, then ORDER BY puts 9 after 10 and SUM quietly skips the ones it can't read. Fix it with CAST(REPLACE(amount, ',', '') AS REAL), or add a real column and UPDATE it from the old one. Take a screenshot of the typeof counts before and after. Photo
  4. 4
    Convert a column of dates like 3/4/2024 into ISO format and show that a date range filter now works. ISO is YYYY-MM-DD, the only format that sorts correctly as text and works with every date function. In SQLite you rebuild it with substr() and the || operator; in DuckDB, strptime() does it in one call. Decide whether 3/4 is March 4 or April 3 by finding a row where the day is above 12. Take a screenshot of the old and new columns together and a BETWEEN query that returns rows. Photo
  5. 5
    Use COALESCE to label the NULL group in a report and show what it was hiding. A GROUP BY category where some categories are NULL makes a group called NULL that most people scroll past. Rewrite it as GROUP BY COALESCE(category, 'unknown') and look at how big that group is. Then compare SUM(tip) with SUM(COALESCE(tip, 0)), which match, and AVG(tip) with AVG(COALESCE(tip, 0)), which don't. Take a screenshot of the report with the unknown group in it. Photo
  6. 6
    Write five lines on which trap was hiding in your file and what it had done to a total. Post the lines with a screenshot of the corrected total next to the wrong one, with the difference between them written out. Photo