A query that nests three subqueries inside each other works, and nobody wants to read it six months later. Common table expressions, subqueries and temporary tables can all hold an intermediate result, and the right choice depends on readability, reuse and how your database executes them.
What a CTE is
A common table expression, or CTE, is a named result set defined at the top of a query with the WITH keyword. You can then refer to it by name in the main query, as if it were a table that exists only for that one statement.
WITH recent_orders AS (
SELECT customer_id, SUM(amount) AS total
FROM orders
WHERE order_date >= DATE '2026-01-01'
GROUP BY customer_id
)
SELECT c.name, r.total
FROM customers c
JOIN recent_orders r ON r.customer_id = c.id;
The main advantage is readability: you can break a long query into named steps and read it from top to bottom.
What a subquery is
A subquery is a query nested inside another, in the FROM, WHERE or SELECT clause. For short, one-off needs, such as filtering with WHERE id IN (SELECT ...), it is perfectly fine and keeps everything in one place. It becomes hard to follow when you nest several of them, or when a correlated subquery, which depends on the outer row, is rerun for every row. Related trade-offs appear in window functions vs subqueries.
What a temporary table is
A temporary table stores an intermediate result for the length of your session, and you can query it many times, add an index to it, and inspect it while debugging. That makes it the better choice when an expensive intermediate result is reused several times, or when it is large enough that you want the database to treat it as a real table, as covered in SQL indexes.
Recursive CTEs
A recursive CTE refers to itself, which makes it the standard tool for hierarchies such as an org chart or a category tree. It starts from a base set of rows and repeatedly adds the rows connected to them until nothing new appears. Subqueries and temporary tables cannot do this as cleanly.
How databases actually run them
A CTE is not automatically a performance tool or a performance problem, and it varies by database. In PostgreSQL 12 and later, a simple non-recursive CTE that is referenced once is normally inlined into the main query, and you can force materialization or inlining with the MATERIALIZED and NOT MATERIALIZED keywords. Older PostgreSQL versions always materialized the CTE, and other databases make different choices. Check the query plan for your own database and version instead of assuming.
How to choose
- Use a CTE to name the steps of a long query and make it readable, or to write a recursive query.
- Use a subquery for a short, simple filter or lookup that does not need a name.
- Use a temporary table when an intermediate result is large, expensive or reused many times, or when you want to index or inspect it.
- Use a view when the same logic should be reusable across many queries and users.
CTEs in pipelines and models
In transformation code, CTEs are the usual way to structure a model: one CTE per step, such as selecting source columns, cleaning them and then joining. This keeps each step reviewable and testable, which supports the checks in how to test a data pipeline. Joining the steps together correctly is covered in SQL joins explained.
A quick checklist
- Is the query long enough that named steps would help a reader?
- Is an intermediate result reused several times, which may justify a temporary table?
- Do you need recursion for a hierarchy?
- Have you checked the query plan in your database, not assumed?
- Would a view serve the same logic across many queries?
If you want help tidying slow or hard-to-read SQL in a pipeline, see my ETL and ELT engineering service.
FAQ
Is a CTE faster than a subquery?
Not by default. Many databases treat them similarly, and the real difference depends on your database, its version and the query plan. A CTE mainly improves readability.
What is the difference between a CTE and a temporary table?
A CTE exists only for one statement and is not stored for reuse. A temporary table persists for the session, can be queried repeatedly and can be indexed.
What is a recursive CTE used for?
It is used for hierarchical or graph-like data, such as org charts, category trees and bills of materials, where each row connects to others.

