Temporary Tables and the WITH Clause in SQL#
š Analyze Data š Validation & Temporary Tables Lesson 029
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Holding intermediate results#
Complex analysis often needs to compute an intermediate result and then work with it ā and writing everything as one deeply nested query becomes unreadable. SQL offers ways to hold intermediate results: temporary tables and, most usefully, the WITH clause (common table expressions). They let you break a complex analysis into named, readable steps, which is essential for the advanced queries this stage covers.
The WITH clause (common table expressions)#
The WITH clause defines a named, temporary result set that exists for the
duration of a single query ā a common table expression (CTE):
WITH regional_revenue AS (
SELECT region, SUM(amount) AS revenue
FROM orders
GROUP BY region
)
SELECT region, revenue
FROM regional_revenue
WHERE revenue > 10000
ORDER BY revenue DESC;
The WITH block computes regional_revenue (revenue per region); the main
query then treats it as if it were a table ā filtering and ordering it. This does
the same work as a subquery in the FROM clause, but named and readable: the
intermediate step has a name and stands on its own, rather than being buried inside
the outer query.
Why CTEs improve complex queries#
CTEs make complex analysis manageable in ways that matter:
Readability ā each step is named and separate, so a multi-step analysis reads top to bottom as a sequence rather than as nested parentheses read inside-out.
Reuse within the query ā a CTE can be referenced multiple times in the main query, computing it once.
Chaining ā multiple CTEs can build on each other (
WITH a AS (...), b AS (SELECT ... FROM a)), expressing a genuine multi-step pipeline as a readable sequence.Debugging ā each CTE can be tested on its own by selecting from it, isolating where a problem is (the isolate step of the problem-solving framework).
For analytical queries of any complexity, CTEs are the standard way to keep them comprehensible.
Temporary tables#
A temporary table is a table that exists only for a session, holding intermediate results that persist across multiple queries (unlike a CTE, which lives only within one query). When an analysis runs several queries against the same intermediate result, computing it once into a temporary table and querying that repeatedly is more efficient than recomputing it each time. The next lesson covers creating them in detail.
The caveat#
CTEs and temporary tables serve overlapping but distinct needs, and choosing wrongly costs clarity or performance. A CTE lives for one query and is ideal for structuring a single complex query readably; a temporary table persists across a session and suits reusing an intermediate result across several queries. Overusing either ā wrapping trivial queries in CTEs, or creating temp tables for one-time use ā adds complexity without benefit. And in some databases a CTE is recomputed each time it is referenced, which can be slow if referenced repeatedly on large data (where a temp table would be better). Match the tool to whether the intermediate result is used once or many times. The next lesson details creating temporary tables.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/11/26/temporary-tables-and-the-with-clause-in-sql/ (insightful-data-lab.com).