MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

A Guide to the CREATE TEMPORARY TABLE SQL Statement

intro

Let's introduce everything you need to know about the CREATE TEMPORARY TABLE SQL statement, including which databases support it and how to use it effectively.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Temporary tables are a useful mechanism supported by most databases to temporarily store data for reuse in scripts or to simplify complex queries. Keep in mind that each database has its own way of handling temporary tables. Now, how does the CREATE TEMPORARY TABLE SQL statement work, and which databases support it? Read this guide to find out!

Temporary Tables in SQL: What Are They and Why Are They Useful?

While this article focuses specifically on the CREATE TEMPORARY TABLE statement, it is useful to take a moment to explain what temporary tables are, when they are used, and why they are important.

Definition

In SQL, a temporary table is a short-lived table that exists only for the duration of a session or transaction—depending on the database and specified configurations. These tables are session-specific, meaning other users cannot access them. Once the session ends, the temporary table is automatically deleted.

Benefits and Usage

Temporary tables introduce several benefits, such as:

  • Improved query performance: When working with complex queries that involve joining multiple large tables, temporary tables can simplify the process and improve performance by reducing redundant calculations and aggregations. For more insights, read our guide on SQL query optimization.
  • Session-specific data storage: They store intermediate results for the current session or transaction, preventing the need for persistent storage and reducing unnecessary data persistence.
  • Isolation: As temporary tables are only visible within the session that created them, they ensure no conflicts with other users' data, providing a clean workspace.
  • Reduced redundancy: Temporary tables help in breaking down complex queries into smaller, more manageable parts, improving readability and making it easier to debug or optimize specific sections of a query.

In particular, some common use cases for temporary tables are:

  • Storing intermediate results: In multi-step operations, temporary tables can store results temporarily before finalizing the operation.
  • Simplifying complex queries: Complex queries with multiple joins can be simplified using temporary tables, making the overall query more efficient.
  • Transaction management: They are often used in cases where changes need to be tested before being committed to the database.

CREATE TEMPORARY TABLE SQL Query: Syntax and Support

Disclaimer: Not all databases support the CREATE TEMPORARY TABLE SQL statement. You can see a breakdown of the most popular databases that support it below:

DatabaseSupports CREATE TEMPORARY TABLE?
MySQL
PostgreSQL
SQL Server❌ (Uses CREATE TABLE #table_name instead)
Oracle❌ (Uses CREATE GLOBAL TEMPORARY TABLE instead)
MariaDB
SQLite

As you can see, SQL Server and Oracle are the major databases that do not support CREATE TEMPORARY TABLE. These databases use alternative methods for creating temporary tables. You can learn more in our general guide on creating temporary tables in SQL.

Time to focus in more detail on how MySQL and PostgreSQL support the CREATE TEMPORARY TABLE SQL command!

MySQL

This is the syntax for creating a temporary table in MySQL:

Copy
        
1 CREATE TEMPORARY TABLE table_name ( 2 column_1 datatype_1, 3 column_2 datatype_2, 4 -- ... 5 );

In detail, this query works just like a regular CREATE TABLE statement. The key difference is the TEMPORARY keyword, which keyword tells MySQL to create the table as a temporary one.

Notes:

  • A TEMPORARY table is visible only within the current session, and it is automatically dropped when the session ends.
  • Different sessions can use the same temporary table name without conflict.
  • If a non-TEMPORARY table exists with the same name as a temporary table, the non-TEMPORARY table is temporarily hidden until the temporary table is dropped.
  • InnoDB does not support compressed temporary tables.
  • Usually, CREATE TABLE would cause an implicit commit, as it is an SQL DDL command. That does not happen when used with the TEMPORARY keyword.
  • Dropping a database does not automatically drop any TEMPORARY tables within it.
  • You must have the CREATE TEMPORARY TABLES privilege to create temporary tables.
  • You cannot use CREATE TEMPORARY TABLE ... LIKE to create a table based on one in the MySQL tablespace.

PostgreSQL

Here is how you can use the CREATE TEMPORARY TABLE SQL statement to create a temporary table in PostgreSQL:

Copy
        
1 CREATE TEMPORARY TABLE table_name ( 2 column_1 datatype_1, 3 column_2 datatype_2, 4 -- ... 5 );

This syntax works just like a regular CREATE TABLE statement. The main difference is the TEMPORARY keyword, which indicates the creation of a temporary table. In PostgreSQL, the TEMPORARY keyword can also be replaced by TEMP.

For more information, read our guide on temporary tables in Postgres.

Notes:

  • Temporary tables exist in a special schema, so no schema name can be provided when creating them.
  • Temporary tables are automatically dropped at the end of the session, or optionally at the end of the current transaction.
  • Indexes created on temporary tables are also temporary.
  • PostgreSQL’s behavior differs from the SQL standard, which defines temporary tables to have the same structure across all sessions.
  • The SQL standard distinguishes between global and local temporary tables, but this is not relevant in Postgres.

CREATE TEMPORARY TABLE Example

Note: The example below will be in MySQL, but the queries also work in PostgreSQL.

Suppose you have an ecommerce database and you want to run the following query:

Copy
        
1 SELECT 2 c.customer_id, 3 c.first_name, 4 c.last_name, 5 SUM(oi.quantity * oi.price) AS total_spent 6 FROM 7 customers c 8 JOIN 9 orders o ON c.customer_id = o.customer_id 10 JOIN 11 order_items oi ON o.order_id = oi.order_id 12 JOIN 13 products p ON oi.product_id = p.product_id 14 WHERE 15 p.category_id = 5 16 AND o.order_date BETWEEN '2025-01-01' AND '2025-12-31' 17 GROUP BY 18 c.customer_id, c.first_name, c.last_name;

The above query calculates the total amount spent by each customer on products in the category with category ID 5 during the year 2025. To do that, it joins four tables—customers, orders, order_items, and products—to combine the relevant data, then filters the results to include only orders made within the specified date range. Finally, it uses GROUP BY to group the results by the customer and sum up their total expenditure.

The result of the query in a multi-database client like DbVisualizer would look something like this:

Note the resulting record set
Note the resulting record set

To simplify the complex query above, you can introduce a temporary table using the CREATE TEMPORARY TABLE SQL statement below:

Copy
        
1 CREATE TEMPORARY TABLE temp_customer_spending AS 2 SELECT 3 c.customer_id, 4 c.first_name, 5 c.last_name, 6 o.order_id, 7 oi.product_id, 8 oi.quantity, 9 oi.price, 10 o.order_date, 11 p.category_id 12 FROM 13 customers c 14 JOIN 15 orders o ON c.customer_id = o.customer_id 16 JOIN 17 order_items oi ON o.order_id = oi.order_id 18 JOIN 19 products p ON oi.product_id = p.product_id;

The temporary table temp_customer_spending consolidates the necessary sales data from the four different tables. With this temporary table in place, you do not need to repeatedly join these tables in the main query.

Execute the CREATE TEMPORARY TABLE query to create the table in the database session:

Executing the CREATE TEMPORARY TABLE in DbVisualizer
Executing the CREATE TEMPORARY TABLE in DbVisualizer

You can now run a much simpler query to get the same result as before:

Copy
        
1 SELECT 2 customer_id, 3 first_name, 4 last_name, 5 SUM(quantity * price) AS total_spent 6 FROM 7 temp_customer_spending 8 WHERE 9 category_id = 5 AND order_date BETWEEN '2025-01-01' AND '2025-12-31' 10 GROUP BY 11 customer_id, first_name, last_name;

As you can verify in DbVisualizer or any other visual database client, you can now get the same result as before but with a much easier-to-understand query:

Note that the result is the same as before but with a much simpler query
Note that the result is the same as before but with a much simpler query

Note that the result is the same as before but with a much simpler query

Wonderful! You just saw how powerful the CREATE TEMPORARY TABLE statement is.

Conclusion

In this blog post, you learned more about the CREATE TEMPORARY TABLE SQL statement. You understood what temporary tables are, why they are useful, and how to use them in MySQL and PostgreSQL. Through an example, you also saw how much easier it is to handle complex queries using a fully-featured database client like DbVisualizer. Download it for free today!

FAQ

Is CREATE TEMPORARY TABLE part of the SQL ANSI/ISO standard?

The CREATE TEMPORARY TABLE SQL statement is part of the SQL ANSI/ISO standard, though it was added in later revisions. While it is standardized, different databases have implemented their own variations for creating temporary tables. As a result, the behavior and syntax can differ a lot across systems.

What is the difference between a regular table and a temporary table?

A regular table exists persistently within a database, storing data for the long term, and it must be explicitly dropped if no longer needed. In contrast, a temporary table exists only for the duration of a session or transaction. Once the session or transaction ends, the temporary table is automatically dropped.

Which databases support the CREATE TEMPORARY TABLE SQL statement?

Several popular databases support the CREATE TEMPORARY TABLE statement, including:

Why use a visual database client?

Using a visual database client like DbVisualizer to create a temporary table offers several advantages, making data management much easier compared to using command-line tools. With DbVisualizer, you can visually see your queries, monitor execution, and access powerful features like drag-and-drop query creation and query optimization. Grab a 21-day free trial for the Pro version!

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

Best Database Tools for Analysts: Complete List

author TheTable tags BI Data analysis SQL 7 min 2025-09-30
title

The HEAP Data Structure and in-Memory Data Explained

author Lukas Vileikis tags MySQL SQL 5 min 2025-09-24
title

SQL Boolean Type: How to Use It in All Major Relational Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-09-23
title

How Dirty Data Pollutes Your Database

author Lukas Vileikis tags SQL 5 min 2025-09-22
title

Best Database Tools for Developers: Ultimate List

author Antonello Zanini tags Developer tools SQL 9 min 2025-09-17
title

Implementing Version Control for Your Database

author Lukas Vileikis tags SQL 4 min 2025-09-16
title

Postgres List Schemas: 3 Different Approaches

author Antonello Zanini tags POSTGRESQL 5 min 2025-09-15
title

JSON_EXTRACT MySQL Function: Complete Guide

author Antonello Zanini tags MySQL 6 min 2025-09-10
title

Listing Tables in Oracle: Three Different Approaches:

author Leslie S. Gyamfi tags ORACLE 8 min 2025-09-09
title

What Happens When You Use the UNION and DISTINCT SQL Clauses Together?

author Lukas Vileikis tags SQL 5 min 2025-09-08

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.