MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

The SELECT INTO TEMP TABLE Mechanism in SQL

intro

Let’s explore everything you need to know about the SQL SELECT INTO TEMP TABLE mechanism in MySQL, PostgreSQL, SQL Server, and Oracle.

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

Sometimes, you need to create a new table, but you already have all the data you need stored in the database. That is especially common when working with temporary tables. In these cases, the goal is to create and populate a temporary table with useful aggregated data to simplify a complex query. That is exactly what the SQL SELECT INTO TEMP TABLE mechanism is designed for.

Follow this guide to learn how to use SELECT INTO with temporary tables to define and fill them in a single SQL statement!

What Does It Mean to Select into a Temp Table in SQL?

Before answering this question, you need to understand what the SQL SELECT INTO clause does and what temporary tables are.

In short, the SELECT INTO statement copies data from one table into a new table. Instead, a temporary table (often called a “temp table”) is a special type of table that exists only for the duration of your session or transaction.

So, selecting into a temp table means creating a short-lived temporary table and immediately populating it with the result of a query. In other words, this is a mechanism to quickly define and fill a new temporary table.

SQL SELECT INTO TEMP TABLE Syntax

The SQL SELECT INTO TEMP TABLE mechanism does not follow a universal syntax. In particular, each major database system implements it differently.

Let’s explore how to perform this operation in MySQL, PostgreSQL, SQL Server, and Oracle!

MySQL

MySQL does not support the SELECT INTO syntax (though it does support INSERT INTO). Thus, you cannot use the SELECT INTO TEMP TABLE pattern in MySQL.

On the contrary, you can achieve the same result using the following syntax:

Copy
        
1 CREATE TEMPORARY TABLE temp_table AS 2 SELECT column_1, column_2, column_n 3 FROM original_table 4 [...];

This creates a temporary table called temp_table and populates it with data from original_table. Note that the AS keyword is followed by a standard MySQL SELECT statement, which can include WHERE, GROUP BY, HAVING, ORDER BY clauses, and more.

PostgreSQL

PostgreSQL supports the SQL SELECT INTO syntax directly, unlike MySQL. So, you can use the Postgres SELECT INTO TEMP TABLE mechanism with the syntax outlined below:

Copy
        
1 SELECT column_1, column_2, ..., column_n 2 INTO temp_table 3 FROM original_table 4 [...];

This will create a temporary table in Postgres, with the data retrieved from the original_table table as specified in the FROM section of the query.

Alternatively, PostgreSQL also supports the CREATE TEMP TABLE syntax, similar to MySQL:

Copy
        
1 CREATE TEMPORARY TABLE temp_table AS 2 SELECT column_1, column_2, column_n 3 FROM original_table 4 [...];

This approach also creates a temporary table and populates it with the results of the SELECT query.

SQL Server

The SQL Server SELECT INTO TEMP TABLE syntax is:

Copy
        
1 SELECT column_1, column_2, ..., column_n 2 INTO #temp_table 3 FROM original_table 4 [...];

Note: In SQL Server, the # prefix denotes a local temporary table, which is only accessible within the current session. The ## prefix denotes a global temporary table, which is accessible to all sessions until the creating session ends. Learn more in our dedicated guide on the SQL Server temporary table mechanism.

Oracle

Oracle supports the SELECT INTO statement, but only to select values into variables within PL/SQL, not to create tables.

To create and populate a temporary table in Oracle, you must first create the table explicitly:

Copy
        
1 CREATE GLOBAL TEMPORARY TABLE temp_table ( 2 column_1 datatype, 3 column_2 datatype, 4 ... 5 column_n datatype, 6 ) ON COMMIT PRESERVE ROWS;

Then, insert data into it with a separate INSERT INTO ... SELECT statement:

Copy
        
1 INSERT INTO temp_table 2 SELECT column_1, column_2 3 FROM original_table 4 [...];

As of the time this blog is written, Oracle does not support a direct SELECT INTO TEMP TABLE mechanism for creating and populating temporary tables in a single step.

SELECT INTO TEMP TABLE: Examples

To better understand how this mechanism works, let’s walk through some practical examples!

The following queries in this section will be written for PostgreSQL, which supports both SELECT INTO TEMP TABLE and CREATE TEMPORARY TABLE ... AS syntax. Still, you can easily adapt them to MySQL or SQL Server using the syntax variations explained earlier in this guide.

Note: These sample queries below will be run in DbVisualizer, a top-rated database client that supports over 50 databases. However, you can run them in any PostgreSQL client.

In detail, the original table we will use to create temporary tables from is called orders:

Exploring the data in the orders table in DbVisualizer
Exploring the data in the orders table in DbVisualizer

This table contains sample order data. The goal is to aggregate this data to calculate the total orders per day and store the results in a temporary table.

Time to see how to achieve that!

SELECT INTO TEMP TABLE

Create a temporary table that aggregates the total order amount by day using SELECT INTO:

Copy
        
1 SELECT customer_id, DATE(placed_at) AS order_date, SUM(total) AS daily_total 2 INTO TEMP TABLE daily_sales_summary 3 FROM orders 4 GROUP BY customer_id, DATE(placed_at) 5 ORDER BY order_date;

Execute the above query:

Executing the SELECT INTO TEMP TABLE query in DbVisualizer
Executing the SELECT INTO TEMP TABLE query in DbVisualizer

As you can see in DbVisualizer, this creates a temporary table with six rows.

You can now write queries like this:

Copy
        
1 SELECT * FROM daily_sales_summary 2 WHERE order_date = '2025-01-01';
Note the aggregated results
Note the aggregated results

That retrieves the aggregated daily order data for January 2025 in a simplified way by using a temporary table created and populated with the SELECT INTO mechanism.

CREATE TEMPORARY TABLE ... AS

Equivalently, you can achieve the same result as before using the CREATE TEMPORARY TABLE ... AS syntax:

Executing the equivalent CREATE TEMPORATY TABLE ... AS query in DbVisualizer
Executing the equivalent CREATE TEMPORATY TABLE ... AS query in DbVisualizer

This will contain:

Take a look at the aggregated data in the temporary table
Take a look at the aggregated data in the temporary table

Et voilà! Mission complete.

Conclusion

In this guide, you learned how the SELECT INTO TEMP TABLE mechanism works across the most popular SQL databases. Specifically, it allows you to create a temporary table and immediately populate it with data. You also discovered how to use this feature in MySQL, PostgreSQL, SQL Server, and Oracle.

As shown here, DbVisualizer simplifies writing and executing SQL queries with support for over 50 databases. It also includes powerful features like SQL formatting, ER diagrams, and query optimization tools. Download DbVisualizer for free today!

FAQ

Is the syntax to SELECT INTO temp tables part of the ANSI SQL standard?

No, the syntax for selecting into temporary tables is not part of the ANSI SQL standard. That is because each database system handles temporary tables differently, let alone the SQL SELECT INTO TEMP TABLE syntax.

What is the difference between CREATE TEMPORARY TABLE ... AS and SELECT INTO TEMP TABLE?

Both help you create a temporary table and populate it with query results. The main difference between the two lies in syntax and database support. CREATE TEMPORARY TABLE ... AS SELECT is available in PostgreSQL and MySQL. Conversely, SELECT INTO TEMP TABLE is a clause supported by systems like SQL Server and PostgreSQL.

What is the SQL Server SELECT INTO TEMP TABLE syntax?

In SQL Server, you can create and populate a temporary table in one step using the SELECT INTO syntax as in this example:

Copy
        
1 SELECT customer_id, total_amount 2 INTO #high_value_orders 3 FROM orders 4 WHERE total_amount > 1000;

This query creates an SQL Server local temporary table called #high_value_orders and fills it with data from the orders table, including only rows where the total_amount exceeds 1000.

Why use a database client?

A visual database client such as DbVisualizer streamlines database tasks by making it easy to query, manage, and visualize data. It provides a user-friendly interface for working with tables, exploring schema relationships, and crafting or troubleshooting queries through its powerful built-in SQL editor. Notable features like autocomplete, ER diagrams, and data export utilities help DbVisualizer stand out. Try the Pro version with a 21-day free trial!

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

MySQL LOCATE Function: Find Substring Position

author Antonello Zanini tags MySQL 7 min 2025-10-22
title

Parsing and SQL Data Types: A Complete Guide

author Lukas Vileikis tags MySQL SQL 6 min 2025-10-21
title

Best SQL Clients for Developers: Complete List

author Antonello Zanini tags Database clients SQL 15 min 2025-10-08
title

Best Database Tools for Business Users: Complete List

author TheTable tags BI SQL 7 min 2025-10-07
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

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.