Lesson 3 of 12
Structured learning draftTreat missing amounts as unknown
A missing sale amount is different from an amount of zero. Identify NULL explicitly and report what is missing before choosing any default, so an incomplete record does not silently become a confirmed zero-value transaction.
Learning objectives
- Find a missing value with IS NULL.
- Distinguish zero from unknown.
- Explain when a default is justified.
Treat missing amounts as unknown: the working sequence
Trace the three stages of treat missing amounts as unknown and compare them with the example.
Sale 4 has no recorded amount, represented by NULL.
IS NULL identifies that missing-value state explicitly.
The report flags the record for follow-up instead of inventing its amount.
Use IS NULL to detect missing values. Comparing a value to NULL with = does not produce true. COALESCE can provide a default, but the default must follow an explicit reporting rule rather than hiding missing data.
WITH sales(id, customer, amount, status) AS (
VALUES (1, 'Amina', 120, 'paid'),
(2, 'Amina', 80, 'pending'),
(3, 'Brian', 50, 'paid'),
(4, 'Chen', NULL, 'paid')
)
SELECT id, customer
FROM sales
WHERE amount IS NULL
ORDER BY id;id | customer
4 | ChenTry it, then change it
- Replace IS NULL with = NULL and inspect the empty result.
- Add a sale with amount 0 and confirm it is not selected by IS NULL.
- Select COALESCE(amount, 0) alongside amount and explain what meaning the substituted zero does and does not establish.
Common mistakes
- Using = NULL to find missing values.
- Replacing unknown values with zero without a documented rule.
- Assuming NULL is an empty string.
Practice activity
Apply the lesson
Create a completeness report with sale identifier, original amount and a missing-amount flag. Include one zero amount and one NULL amount. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · databases
A join returns more rows than expected.
- Find the cause
- Count matching records for each join key in both tables. A key repeated on both sides creates multiple combinations.
- Try this
- Decide the intended row grain first: one row per customer, order or item. Aggregate at that grain or correct the join condition; do not hide the problem with DISTINCT.
- Check the fix
- Use two customers and three orders as a fixture. Predict every result row and check unmatched records separately.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: databases with SQL
Watch Querying or Relating to see selection and joins explained using actual tables. Examples begin with SQLite; check syntax when using PostgreSQL.
Try it yourself: Draw two small tables, predict the joined rows, then compare your prediction with the query result.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
Which predicate finds the fixture's missing amount?
Lesson summary
- NULL marks missing or unknown information.
- IS NULL tests that state explicitly.
- A replacement value is a reporting choice, not recovered evidence.
Sources and further reading
- Comparison predicatesPostgreSQL Global Development Group - accessed 2026-10-07
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.