Module 06 Summary
The idea this module was built around
A long query is not hard because it is long. It is hard because you cannot tell which part of it is wrong. Staged CTEs fix that by making every intermediate step separately runnable and separately checkable.
What you can now do
- Decompose a question into named stages, each with a stated grain
- Run a checkpoint after every stage rather than debugging the whole thing at the end
- Choose between a subquery, a CTE, and a join on structural grounds
The trap this module removed
Correctness you cannot demonstrate is indistinguishable from luck. A four-line comment above each CTE saying what it produces and at what grain does more for reviewability than any amount of formatting.
The finding this module handed you
Reconciling line-item value against order_total fails - for 999 of the 1,000 orders. Only order 501 agrees, at ₹1,000.00 either way. This is not something to fix in your query. Order value and basket detail are recorded by different processes in this system, so neither is derivable from the other.
The professional response is a sentence in your report: which measure you used, and what the discrepancy means for the questions your analysis cannot answer. Silently choosing the number that looks better is how analysts lose the trust that took them years to earn.
Figures worth remembering
Stage row counts: 4,812 → 1,000 → 1,000 → 1,000. If the last one is 3,400, you joined line items directly instead of rolling them up first.
Before you move on
Rewrite your most complicated recent query as staged CTEs with a row-count comment per stage. It usually takes ten minutes and it usually finds something.
