// dialect comparison

SELECT INTO vs CREATE TEMP TABLE AS

SQL Server says SELECT INTO, three engines say CREATE TEMP TABLE AS, and Oracle needs a special prefix.

Filed under Querying

The verdict

CREATE TEMPORARY TABLE … AS SELECT covers PostgreSQL, MySQL and SQLite; SQL Server's SELECT INTO #name is its own dialect. Oracle can do the same since 18c with a private temporary table, but the required ORA$PTT_ name prefix means the statement never ports as is.

Where they differ

SQL Server builds a temp table with SELECT … INTO #tmp. PostgreSQL, MySQL and SQLite use CREATE TEMP(ORARY) TABLE … AS SELECT. Oracle 18c+ has private temporary tables, whose names must start with ORA$PTT_; before that, a global temporary table had to exist in advance.

How each engine writes it

SQL Server

SELECT id, total INTO #big_orders FROM orders WHERE total > 1000;

# marks a session-local temp table in tempdb, dropped when the session ends; ## makes it global. There is no CREATE TEMP TABLE … AS.

PostgreSQL

CREATE TEMP TABLE big_orders AS SELECT id, total FROM orders WHERE total > 1000;

Lives until the session ends. ON COMMIT DROP or DELETE ROWS ties it to the transaction instead. SELECT … INTO TEMP big_orders FROM … also works.

MySQL

CREATE TEMPORARY TABLE big_orders AS SELECT id, total FROM orders WHERE total > 1000;

MySQL's SELECT … INTO writes to variables or files, not tables. A TEMPORARY table is visible only to its session and hides a permanent table of the same name.

SQLite

CREATE TEMP TABLE big_orders AS SELECT id, total FROM orders WHERE total > 1000;

TEMP tables live in the connection's separate temp database. Without TEMP the table is permanent.

Oracle

CREATE PRIVATE TEMPORARY TABLE ora$ptt_big_orders ON COMMIT PRESERVE DEFINITION AS SELECT id, total FROM orders WHERE total > 1000;

18c+. The ORA$PTT_ prefix is required by default; ON COMMIT DROP DEFINITION (the default) removes it at commit. A plain CREATE TABLE … AS makes a permanent table.

Questions that come up

How long does a temp table live?

SQL Server #tables: the session, or the stored procedure that created them. PostgreSQL: the session, or the transaction with ON COMMIT DROP. MySQL and SQLite: the connection. Oracle private temporary tables: the transaction or the session, per ON COMMIT; global temporary tables have a permanent definition with per-session rows.

Why does my CTAS temp table have no indexes or constraints?

CREATE TABLE … AS (and SELECT INTO) copies column names, types and data, not indexes, primary keys or defaults, on every engine. Add the indexes afterwards, or create the table first and fill it with INSERT INTO … SELECT.

Is a CTE a substitute for a temp table?

For a single statement, often. A CTE exists only inside that statement, can't be indexed, and depending on the engine may be recomputed for each reference. Multi-step work that reads the intermediate result several times is better served by a temp table.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.