Previous module: Module 9, When a join increases your numbers.
A long query isn’t always a complicated query. Sometimes it contains several small tasks that have been placed inside one another. The SQL can still return the correct answer, but it becomes difficult to see which rows were filtered, when the grain changed, and where a wrong number first appeared.
Subqueries and common table expressions, usually called CTEs, help us separate those tasks. A subquery places one query inside another. A CTE gives a query result a name with WITH so that we can read and test the steps in order.
In this module, we’ll rewrite one nested query as prep, metrics, and final. We will also check the number of rows at each stage and confirm that the new query returns the same result as the old one.



