Lesson 11 of 12
Structured learning draftReconcile included and excluded records
Before sharing a paid-sales report, account for the records it excluded. Build a small count summary that explains the original population and the status rule, so a missing record cannot be confused with a disappeared or duplicated sale.
Learning objectives
- Account for input records under an explicit rule.
- Count matching rows with a conditional expression.
- Check that included and excluded counts sum to the population.
Reconcile included and excluded records: the working sequence
Trace the three stages of reconcile included and excluded records and compare them with the example.
The population begins with four sales records.
The status conditions classify each row into paid or other.
Three paid plus one other reconciles to all four original records.
Conditional counts separate paid and other statuses. COUNT(CASE WHEN ... THEN 1 END) excludes the NULL returned for non-matching rows. Treat missing status as its own policy decision; this example includes it in the other-status count.
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 COUNT(*) AS all_records,
COUNT(CASE WHEN status = 'paid' THEN 1 END) AS paid,
COUNT(CASE WHEN status <> 'paid' OR status IS NULL THEN 1 END) AS other
FROM sales;all_records | paid | other
4 | 3 | 1Try it, then change it
- Check the three counts by listing the four rows.
- Add a NULL status and predict all three new counts.
- Write a separate unknown-status count if the report needs that distinction, and reconcile all categories.
Common mistakes
- Using overlapping categories and double-counting records.
- Leaving NULL status outside every category.
- Checking a total without first stating the population.
Practice activity
Apply the lesson
Create paid, pending and unknown-status categories that do not overlap. Prove that their counts sum to the source count and document any unrecognised status. 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
With disjoint, exhaustive categories, what should their counts sum to?
Lesson summary
- Every input record should have an explained reporting treatment.
- Categories need explicit and non-overlapping rules.
- A reconciliation compares the report with independent source counts.
Sources and further reading
- SELECT processing and groupingPostgreSQL 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.