What Is a SQL CTE?

A Common Table Expression (CTE) is a temporary, named result set defined with the WITH keyword that exists only for the duration of a single SQL statement. CTEs improve readability, enable recursion, and let you reference the same intermediate result multiple times without repeating code.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
A Common Table Expression (CTE) is a temporary, named result set created with WITH that lives only for the current query. CTEs simplify complex logic, support recursion, and boost readability by isolating sub-queries.

What Is a SQL CTE?

A Common Table Expression (CTE) is a temporary, named result set defined with the WITH keyword that exists only for the duration of a single query. Databases treat a CTE like an inline view, letting you reference it multiple times without duplicating logic.

Why Use a CTE Instead of a Subquery?

CTEs improve readability by giving the subquery a descriptive name, reduce duplication when the same derived table is needed more than once, and enable recursion—something regular subqueries cannot do.

How Does CTE Syntax Work?

Place WITH cte_name AS (SELECT ...) before the main SELECT, UPDATE, INSERT, or DELETE statement. You can define multiple CTEs by separating them with commas.

Can I Chain Multiple CTEs?

Yes. Each CTE can reference earlier CTEs in the same WITH clause, allowing you to build complex logic step-by-step while keeping each step short and clear.

How Long Does a CTE Live?

A CTE lives only for the duration of the single statement that follows the WITH clause. After execution, it disappears and consumes no additional memory or storage.

Does a CTE Improve Performance?

CTEs themselves do not guarantee faster queries. They can reduce duplication, which may help the optimizer, but performance depends on execution plans. Treat CTEs as a readability tool first.

What Is a Recursive CTE?

A recursive CTE references itself to iterate over hierarchical data such as org charts or folder trees. It consists of an anchor query that sets the starting rows and a recursive query that repeatedly joins back to the CTE.

When Should I Use a Recursive CTE?

Use recursive CTEs when you need to traverse parent-child relationships of unknown depth, build running totals, or generate sequences without stored procedures.

What Are Common CTE Use Cases?

Typical use cases include simplifying multi-step aggregations, reusing complex joins across several parts of a query, isolating business logic, performing recursion, and preparing data for window functions.

How Do I UPDATE With a CTE?

Most relational databases allow UPDATE, DELETE, or INSERT operations that target the rows returned by a CTE, offering a concise, set-based alternative to cursors.

Best Practices for Writing CTEs

Name each CTE descriptively, keep columns minimal, avoid nesting CTEs too deeply, and test performance by comparing CTEs versus inline subqueries or temp tables.

Is a CTE the Same as a Temp Table?

No. A CTE is virtual and query-scoped, while a temporary table is physically materialized and persists for the session unless dropped. Choose temp tables when you need indexes or repeated access across multiple queries.

How Does a CTE Interact With Window Functions?

CTEs can stage pre-aggregated or filtered data, making subsequent window functions simpler and faster to read by separating concerns into logical steps.

Which Databases Support CTEs?

PostgreSQL, MySQL 8+, SQL Server, Oracle, SQLite, Snowflake, BigQuery, and most modern SQL engines support both non-recursive and recursive CTEs.

Wrap-Up: Key Takeaways

CTEs create named, query-scoped result sets that clarify logic and enable recursion. Use them to avoid duplication, enhance maintainability, and structure complex queries into readable steps. Test performance and follow naming conventions for maximum benefit.

Frequently Asked Questions (FAQs)

Is a CTE the same as a view?

No. A CTE is temporary and query-scoped, while a view is stored in the database catalog and reused across sessions.

Can I nest CTEs inside each other?

You cannot declare a WITH inside another WITH clause, but you can reference earlier CTEs to create a chain—achieving similar layering without nesting.

How many CTEs can I define in one query?

Most databases impose no strict limit; practical limits come from query complexity and readability. Keep the chain manageable.

Are CTEs supported in MySQL?

Yes, starting from MySQL 8.0, both non-recursive and recursive CTEs are fully supported.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo