Non-Brand Data

Non-Brand Data

Module 10: Breaking a query into steps with subqueries and CTEs

Let's learn how to split a long SQL query into named steps and check the result from each step.

Cornellius Yudha Wijaya's avatar
Cornellius Yudha Wijaya
Oct 02, 2026
∙ Paid

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.


User's avatar

Continue reading this post for free, courtesy of Cornellius Yudha Wijaya.

Or purchase a paid subscription.
© 2026 Cornellius Yudha Wijaya · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture