Common Table Expression (CTE) vs Temporary Table in SQL Server

Common Table Expression (CTE) vs Temporary Table in SQL Server

Structured Query Language (SQL) is a standard language used to manage relational databases. SQL Server is a relational database management system (RDBMS) developed by Microsoft. In SQL Server, there are various ways to store and manipulate data, including Common Table Expressions (CTEs) and Temporary Tables. While they might seem similar, there are some fundamental differences between the two.

Common Table Expressions (CTEs)

A CTE is a named temporary result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. A CTE is defined using the WITH statement, and it can reference itself recursively. A CTE is also known as a subquery factoring, and it can be used to simplify complex queries, make them more readable and easier to maintain.

CTEs are not physically stored on disk, and their lifespan is limited to the execution of a single query. This means that you cannot create, alter or drop CTEs explicitly. Also, you cannot reference a CTE from multiple queries within the same batch.

Let’s consider the following example, where we have a table named “Employees” with columns “EmployeeID”, “FirstName”, “LastName”, “DepartmentID”, and “Salary”:

WITH TopEmployees AS (
  SELECT TOP 10 
    EmployeeID,
    FirstName,
    LastName,
    Salary
  FROM Employees
  ORDER BY Salary DESC
)
SELECT 
  FirstName + ' ' + LastName AS FullName, 
  Salary
FROM TopEmployees;

In this example, we’re using a CTE named “TopEmployees” to retrieve the top 10 employees with the highest salary from the “Employees” table. We then select the full name and salary of these top employees from the CTE. The CTE allows us to simplify the query and make it more readable by separating the top employees selection from the final select statement.

Temporary Tables

Temporary tables are also temporary result sets that are stored in the tempdb system database. Unlike CTEs, temporary tables are physically stored on disk, and you can create, alter or drop them explicitly. Temporary tables can be used to store and manipulate large amounts of data and can be used in multiple queries within the same batch.

Temporary tables can be created using the CREATE TABLE statement with the prefix “#” or “##” for local and global temporary tables, respectively. Local temporary tables are only accessible from the current session and are automatically dropped when the session ends. Global temporary tables are accessible from all sessions, and they are dropped automatically when the last session referencing them is closed.

Let’s consider the following example, where we want to create a temporary table to store the top 10 employees with the highest salary:

CREATE TABLE #TopEmployees (
  EmployeeID INT,
  FirstName NVARCHAR(50),
  LastName NVARCHAR(50),
  Salary DECIMAL(10,2)
);

INSERT INTO #TopEmployees
SELECT TOP 10 
  EmployeeID,
  FirstName,
  LastName,
  Salary
FROM Employees
ORDER BY Salary DESC;

SELECT 
  FirstName + ' ' + LastName AS FullName, 
  Salary
FROM #TopEmployees;

DROP TABLE #TopEmployees;

In this example, we create a temporary table named “#TopEmployees” with columns “EmployeeID”, “FirstName”, “LastName”, and “Salary”. We then insert the top 10 employees with the highest salary from the “Employees” table into the temporary table. Finally, we select the full name and salary of these top employees from the temporary table and drop the table explicitly.

Differences between CTEs and Temporary Tables

The main differences between CTEs and Temporary Tables are:

  1. Storage: CTEs are not physically stored on disk, while temporary tables are.
  2. Lifespan: CTEs exist only for the duration of the query execution, while temporary tables can exist beyond a single query execution.
  3. Explicit Management: You cannot explicitly create, alter, or drop a CTE, while you can with a temporary table.
  4. Scope: CTEs are only accessible within the query that defines them, while temporary tables can be accessed by multiple queries within the same batch.

When to use CTEs vs. Temporary Tables

CTEs are often preferred over temporary tables when you need to simplify complex queries and improve query readability. CTEs are also useful when you need to reference the same result set multiple times within the same query.

Temporary tables are useful when you need to store and manipulate large amounts of data, and you need to reference that data across multiple queries within the same batch. Temporary tables can also improve query performance by allowing you to index and optimize the data in the table.

Conclusion

CTEs and Temporary Tables are both useful tools in SQL Server, but they are different in terms of storage, lifespan, and explicit management. Understanding the differences between the two can help you choose the right tool for the job and optimize your queries for performance and readability.

Share this post

Comment (1)

  • Vladislav Maksimov Reply

    Thank you for the good article explaining the difference between CTE and temporary tables.

    Just one note that can be useful for others:

    • “Temporary result set” is just a logical explanation but not the physical essential of CTE. In most cases when the query is more complex than in the given example, the CTE will be recalculated for each usage. This knowledge is crucial to understanding why, in some cases, CTE can cause significant performance loss, which can be easily fixed by splitting the query using temporary tables.

    For a more detailed explanation and examples see Martin Smith’s answer on SO: https://stackoverflow.com/questions/690465/which-are-more-performant-cte-or-temporary-tables

    September 27, 2023 at 9:33 AM

Leave a Reply

Your email address will not be published. Required fields are marked *