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.
SQL Server says SELECT INTO, three engines say CREATE TEMP TABLE AS, and Oracle needs a special prefix.
Filed under Querying
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
WITH works in all five engines; the details around it don't.
AS is optional almost everywhere, and a syntax error in one place.
Five engines, two spellings of "give me rows 21 to 30", plus Oracle's old ROWNUM wrapper.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.