Common Table Expressions (CTEs) vs Temporary Tables in BigQuery

Abhik Saha
3 min readApr 14, 2023

--

CTEs (Common Table Expressions) and temporary tables are both tools available in SQL for managing and manipulating data. Here’s a comparison of the two based on their efficiencies:

Memory management :
CTEs are typically held in memory, which means that they can be accessed and manipulated quickly. Temporary tables, on the other hand, are usually written to disk, which can cause a performance overhead.

Usage :
CTEs are usually used for smaller data sets that require more complex manipulation. For example, a CTE can be used to calculate running totals or to perform hierarchical queries. Temporary tables, on the other hand, are often used to store larger data sets that require significant manipulation.

Recursion :
CTEs can be used for recursive queries, which allow you to repeatedly apply a set of rules to a data set until a certain condition is met. This can be useful for hierarchical data, such as an organization chart or a bill of materials. Temporary tables can also be used for recursion, but the process is typically more complex.

Scope :
CTEs are only available within the context of a single query. They are not stored permanently, and they cannot be accessed by other queries. Temporary tables, on the other hand, can be created and accessed by any query in the current session. This can be useful for storing intermediate results that need to be shared across multiple queries.

Performance :
In general, CTEs are more efficient than temporary tables for small data sets. This is because CTEs are held in memory and do not require disk access. However, for larger data sets, temporary tables may be more efficient, as they can be indexed and optimized for specific queries.

CTEs and temporary tables are both useful tools for managing data in SQL. The choice between the two will depend on the specific use case, the size of the data set, and the efficiency considerations mentioned above.
On my next post, I will address the same SQL problem using the above two methods.

Here’s an example of using a CTE and a temporary table to solve the same problem:
Problem: Calculate the average salary of all employees who have been with the company for at least 5 years.

CTEs vs Temporary Tables

Explanation for CTE: The CTE “employee_years” is used to calculate the number of years each employee has been with the company, and then joined with the “salaries” table to calculate the average salary of all employees who have been with the company for at least 5 years.

Explanation for Temp tables: The temporary table “employee_years” is created to store the number of years each employee has been with the company, and then joined with the “salaries” table to calculate the average salary of all employees who have been with the company for at least 5 years.

The temporary table is automatically dropped when the session terminates.

--

--

Abhik Saha

Data Engineer @Accenture India || Writes about Bigquery, Cloud Function, GCP, SQL || LinkedIn ID: https://www.linkedin.com/in/abhik-saha-919646108/