Lesson 5 of 12
Structured learning draftKeep customers without orders in a SQL report
A customer report must include people who have never placed an order. Build a small dataset, predict the result and see why a LEFT JOIN preserves those customers while the choice of counting expression changes the answer.
Learning objectives
- Explain how an inner join differs from a left join.
- Count matching orders without counting an unmatched row.
- Check query output using a small, known dataset.
Customers to an order-count report
Customers to an order-count report: a step-by-step view of the code example.
Amina, Brian and Chen must each appear in the final report.
LEFT JOIN finds two orders for Amina, one for Brian and no order for Chen.
COUNT(o.id) returns 2, 1 and 0 because it excludes the unmatched NULL.
WITH customers(id, name) AS (
VALUES (1, 'Amina'), (2, 'Brian'), (3, 'Chen')
), orders(id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 2)
)
SELECT c.name, COUNT(o.id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;| Customer | order_count |
|---|---|
| Amina | 2 |
| Brian | 1 |
| Chen | 0 |
LEFT JOIN keeps each customer even without a matching order. The unmatched order fields become NULL. COUNT(o.id) counts non-NULL order identifiers; COUNT(*) would count the preserved row and incorrectly give Chen a count of 1.
Investigate the report
- Run the complete query in a PostgreSQL scratch database; it creates no permanent tables.
- Replace LEFT JOIN with INNER JOIN and identify the missing customer.
- Restore LEFT JOIN, replace COUNT(o.id) with COUNT(*) and explain Chen's result.
- Add another order for Brian and predict the changed count before running the query.
Common mistakes
- Using INNER JOIN and dropping customers without orders.
- Counting all joined rows with COUNT(*) instead of matching orders.
- Filtering an order field in WHERE and removing unmatched customers.
Practice activity
Apply the lesson
Add a fourth customer with no orders. Produce a report showing all four customers, then a second report showing only customers with no orders. Explain your filter and retain both query outputs.
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 expression counts zero orders for an unmatched customer?
Lesson summary
- LEFT JOIN preserves unmatched customers.
- COUNT(column) excludes NULL values.
- Check the intended row grain and predict the fixture result before extending the report.
Sources and further reading
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
- Joins between tablesPostgreSQL 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.