Lesson 12 of 12
Structured learning draftDeliver a report with zero-order customers included
Finish with a report that includes every intended customer, counts paid orders and makes missing amounts visible. Preserve customers with no matching paid orders, then reconcile the result instead of presenting a table without explaining its rules.
Learning objectives
- Preserve the complete customer population.
- Count orders and missing amounts separately.
- Explain and reconcile a final report.
Deliver a report with zero-order customers included: the working sequence
Trace the three stages of deliver a report with zero-order customers included and compare them with the example.
Three customers are the required output population.
The join matches paid orders without removing customers who have none.
The report distinguishes zero matching orders from a matched order whose amount is missing.
The paid-status condition belongs in the join so it does not remove unmatched customers. COUNT(o.id) measures matches; COUNT(o.amount) measures known amounts. A zero total below represents no known matched amount, so the missing-amount count must stay visible.
WITH customers(id, name) AS (
VALUES (1, 'Amina'), (2, 'Brian'), (3, 'Chen')
), orders(id, customer_id, amount, status) AS (
VALUES (101, 1, 120, 'paid'), (102, 1, 80, 'pending'),
(103, 2, NULL, 'paid')
)
SELECT c.name, COUNT(o.id) AS paid_orders,
COUNT(o.id) - COUNT(o.amount) AS missing_amounts,
COALESCE(SUM(o.amount), 0) AS known_paid_total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id AND o.status = 'paid'
GROUP BY c.id, c.name
ORDER BY c.id;name | paid_orders | missing_amounts | known_paid_total
Amina | 1 | 0 | 120
Brian | 1 | 1 | 0
Chen | 0 | 0 | 0Try it, then change it
- Predict each of the three output rows before running the statement.
- Move the status filter to WHERE temporarily and identify the lost customer; then restore it.
- Reconcile two paid orders, one missing amount and a known paid total of 120 against the source.
Common mistakes
- Moving a right-table filter to WHERE and losing unmatched customers.
- Calling Brian's displayed zero a confirmed zero-value sale.
- Sharing a report without its population, status rule or coverage.
Practice activity
Apply the lesson
Complete the course project with a fourth customer and another paid order. Include the input fixture, expected table, result, missing-data policy and a short reconciliation note. 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
Why does Brian's displayed total of zero need the missing_amounts column?
Lesson summary
- Keep population-preserving filters in the intended join boundary.
- Show missing-value coverage beside substituted totals.
- A report is ready to explain only when its counts and known amounts reconcile.
Sources and further reading
- Joins between tablesPostgreSQL Global Development Group - accessed 2026-10-07
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
Course practical outcome
Produce a customer order report that includes customers with no orders.
Expected output: A runnable query, fixture records, expected-result table and reconciliation note.
Production steps
- Define the output grain as one row per customer.
- Create a small fixture with multiple orders, one unmatched customer and a pending order.
- Join and count paid orders while preserving customers with no matches.
- Check missing values and whether filters change the intended population.
- Reconcile counts and amounts to the source fixture and document the query and limitations.
Success criteria
- Every intended customer appears once.
- Unmatched customers have a zero matching-order count.
- Paid and pending orders are handled by an explicit rule.
- Report totals agree with an independent input calculation.
Next step: Add a fourth customer and an additional order, predict the new result and rerun the reconciliation.
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.