Start studying
Module 5 · SQL Module contents
Module 5  ·  Card 13 / 14intermediate

Common Table Expressions

A common table expression, or CTE, is a temporary named result set defined before a query using the WITH keyword.

It allows data scientists to break complex multi-step queries into modular, sequential blocks that read from top to bottom.

Once defined, the CTE can be referenced in the main query just like a physical database table or view.

Multiple CTEs can be defined in a single statement, separated by commas, allowing subsequent CTEs to reference earlier ones.

Using CTEs improves code readability and maintenance, replacing tangled subqueries with clear logical stages of data transformation.

Learn in depth ↓
Module 05 · SQL · 14 concepts
In depth  /  15 lines

Common Table Expressions, in more detail

A common table expression, or CTE, defines a named temporary result set within the execution scope of a single query.

Introduced in the SQL nineteen ninety-nine standard, CTEs are specified using the WITH keyword before the primary SELECT statement.

They provide a clean, literate programming style for SQL by breaking complex transformations into named sequential steps.

In contrast to deeply nested subqueries, CTEs read from top to bottom, mirroring how human analysts think about data workflows.

A statement can define multiple CTEs separated by commas, where later CTEs can query previously defined CTEs in the sequence.

For example, an initial CTE can clean dates, a second can compute regional totals, and the main query can rank the top regions.

CTEs exist strictly during query execution and do not consume permanent database storage or metadata catalogue space.

Recursive CTEs reference their own output, enabling queries to traverse hierarchical structures like file trees and graph networks.

A recursive CTE consists of an anchor member that seeds the recursion, paired with a recursive member linked by UNION ALL.

The recursion terminates automatically when the recursive query returns an empty result set on the latest iteration.

In many database systems, CTEs are treated as optimisation fences or inline views depending on the query planner engine.

Modern planners inline CTEs into the main query plan, optimising filters and indexes across boundaries seamlessly.

However, some engines historically materialised CTEs unconditionally, which could prevent the engine from pushing down WHERE filters.

Understanding whether your database inlines or materialises CTEs helps you decide between CTEs and temporary tables for big data.

Analysts should use CTEs as the default architecture for complex queries, enhancing code readability, debugging, and peer review.

Want the full technical article? Wikipedia ↗
Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
Why do data analysts frequently use common table expressions (CTEs) instead of deeply nested subqueries?
SQL

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close