DEV Community


Posted on

What Is a Common Table Expression in SQL

Image description

Common Table Expressions (CTE) are statements that yield a temporary result set that can be utilized again within the same sentence. It doesn't take up any disk space and is not stored anywhere because it's a transient result. It can be referred to, therefore, just like any other table.

Alternatively, a CTE could be thought of as a type of virtual table with columns holding actual records. After the main query is executed, this virtual table is formed, utilized by another query, and then removed.

Database professionals have been using CTEs since SQL Server 2005 when they were first introduced. They are used to make complex queries easier to read and manage because naming each CTE is required by SQL rules, which helps the reader understand the entire code.

Learn more:

Top comments (0)