All lessons in Software Engineering: Programs, Systems, and Maintainable Software
SQL Joins, Aggregation, and NULL
Write reports whose join cardinality, filters, and counts match the question.
40 minutes · Members lesson · preview
What you will learn
- Predict row multiplication before aggregating a join.
- Preserve unmatched rows when a report requires them.
- Separate SQL structure from externally supplied values.
The library asks for active-loan counts for every title, including titles with zero active loans. A query joining titles, copies, and loans seems straightforward, but one misplaced filter can remove the zero-count titles. Another extra join can multiply loan rows and inflate the totals.
Write the desired output grain first: one row per title. Then reason about how each join changes row cardinality before adding aggregation.