Lesson 1 of 8 | SQL Reasoning
Start with Cardinality and the Required Result Shape
Outcomes
Reason about how many rows enter and leave each operator before proposing syntax or an index.
Core method
Write the required grain: one row per customer, order, day, or event. Identify filters, join multiplicity, grouping, ordering, and pagination. A one-to-many join can multiply rows before aggregation; DISTINCT may hide that bug while adding work. Push selective filters when semantics allow, project only needed columns, and preserve outer-join intent by placing predicates carefully.
Worked example
To return customers with order totals, aggregate orders by customer first or group after a correct join. If customers with zero orders must remain, use a left join and keep order filters in the join condition or a null-safe expression; a WHERE predicate on the right table can accidentally turn it into an inner join.
Practice and review checklist
- State output grain.
n- Estimate cardinality after each join.n- Check one-to-many multiplication.n- Distinguish filtering rows from filtering groups.n- Verify null and empty-set behaviour.