By Matthew Rathbone · December 6, 2026

SELECT INTO creates a new table and fills it with the rows a query returns, all in one statement. You write a normal SELECT and add INTO new_table just before FROM:

SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE condition;

The new table gets its column names and data types from the query. It must not already exist; to add rows to a table you already have, use INSERT INTO ... SELECT instead.

The catch is that SELECT INTO isn't standard everywhere. It creates a table in SQL Server and PostgreSQL. In MySQL, Oracle and inside stored procedures, SELECT ... INTO stores values in variables instead, and SQLite doesn't support it at all. Those databases use CREATE TABLE ... AS SELECT:

DatabaseCreate a table from a queryWhat SELECT ... INTO does
SQL Server / Azure SQLSELECT ... INTO new_table FROM ...Creates a table
PostgreSQLCREATE TABLE new_table AS SELECT ... (preferred) or SELECT INTOCreates a table (in plain SQL); sets variables (in PL/pgSQL)
MySQL / MariaDBCREATE TABLE new_table AS SELECT ...Sets variables, or writes a file with INTO OUTFILE
OracleCREATE TABLE new_table AS SELECT ...Sets variables (PL/SQL only)
SQLiteCREATE TABLE new_table AS SELECT ...Syntax error
Snowflake, BigQuery, DatabricksCREATE TABLE new_table AS SELECT ...Sets variables in Snowflake Scripting; not a table copy

Jump to: sample data · copy a table · some columns and rows · what gets copied · structure only · from a join or GROUP BY · temp tables · existing table error · vs INSERT INTO SELECT · identity columns · ORDER BY and TOP · another database · PostgreSQL · MySQL · SQLite · Oracle, Snowflake, BigQuery · performance · errors · FAQ

Sample data

The examples use a small shop with customers and orders. The orders table has the things a real table has: an auto-numbered primary key, a foreign key, NOT NULL columns, a default, a CHECK constraint and an index. That matters, because we'll check which of them survive the copy.

CREATE TABLE customers (
  id      INT PRIMARY KEY,
  name    VARCHAR(50) NOT NULL,
  country VARCHAR(30) NOT NULL
);

CREATE TABLE orders (
  id          INT IDENTITY(1,1) PRIMARY KEY,   -- auto-numbered
  customer_id INT NOT NULL REFERENCES customers(id),
  status      VARCHAR(20) NOT NULL DEFAULT 'pending',
  amount      DECIMAL(10,2) NOT NULL CHECK (amount >= 0),
  order_date  DATE NOT NULL
);
CREATE INDEX ix_orders_date ON orders(order_date);

INSERT INTO customers VALUES
  (1, 'Ana', 'Spain'), (2, 'Ben', 'USA'), (3, 'Chloe', 'France'), (4, 'Dev', 'USA');

INSERT INTO orders (customer_id, status, amount, order_date) VALUES
  (1, 'shipped',   120.00, '2026-10-03'),
  (2, 'shipped',    75.50, '2026-10-15'),
  (1, 'cancelled',  40.00, '2026-11-02'),
  (3, 'shipped',   210.00, '2026-11-09'),
  (2, 'pending',    99.99, '2026-11-20'),
  (4, 'shipped',    15.00, '2026-11-28');

That's the SQL Server version. PostgreSQL used GENERATED ALWAYS AS IDENTITY and MySQL used AUTO_INCREMENT for the id column; everything else is identical. Every query here was run on SQL Server 2022 (16.0), PostgreSQL 16.15, MySQL 8.0.46 and SQLite 3.53, and the output and error messages are copied from those runs.

Copy a whole table with SELECT * INTO

The most common use of SELECT INTO is a quick backup before you run a risky UPDATE or DELETE:

SELECT *
INTO orders_backup
FROM orders;
(6 rows affected)

That's all SQL Server prints. There was no CREATE TABLE: the table orders_backup didn't exist a moment ago, and now it has the same columns and all six rows:

SELECT * FROM orders_backup;
 id | customer_id | status    | amount | order_date
----+-------------+-----------+--------+------------
  1 |           1 | shipped   | 120.00 | 2026-10-03
  2 |           2 | shipped   |  75.50 | 2026-10-15
  3 |           1 | cancelled |  40.00 | 2026-11-02
  4 |           3 | shipped   | 210.00 | 2026-11-09
  5 |           2 | pending   |  99.99 | 2026-11-20
  6 |           4 | shipped   |  15.00 | 2026-11-28

PostgreSQL runs the exact same statement and reports SELECT 6. If something goes wrong with the original, you can copy rows back from the backup with INSERT INTO ... SELECT. When you no longer need it, DROP TABLE orders_backup;.

A good habit is to put the date in the name, such as orders_backup_20261206, so you know later what the copy was for and when it's safe to delete.

Copy only some columns and rows

Because SELECT INTO is just a SELECT with an extra clause, you can pick columns and filter rows with WHERE like any other query. The new table contains only what the query returns:

SELECT id, customer_id, amount
INTO shipped_orders
FROM orders
WHERE status = 'shipped';

SELECT * FROM shipped_orders;
(4 rows affected)

 id | customer_id | amount
----+-------------+--------
  1 |           1 | 120.00
  2 |           2 |  75.50
  4 |           3 | 210.00
  6 |           4 |  15.00

The new table has three columns, not five, and four rows, not six. The order of the clauses is fixed: SELECT list, then INTO, then FROM, WHERE, GROUP BY and so on. Putting INTO at the end (SELECT * FROM orders INTO x) is a syntax error in SQL Server: Incorrect syntax near the keyword 'INTO'.

What SELECT INTO copies (and what it doesn't)

This is the part most tutorials skip. SELECT INTO copies the data and the column definitions, but almost none of the rules attached to the table. Here's what happened to each feature of orders when it was copied in each database (SQL Server and PostgreSQL with SELECT * INTO, MySQL and SQLite with the equivalent CREATE TABLE orders_backup AS SELECT * FROM orders):

Feature of the originalSQL ServerPostgreSQLMySQLSQLite
Column namesYesYesYesYes
Data types (VARCHAR(20), DECIMAL(10,2))YesYesYesLoosely (TEXT, NUM)
NOT NULLYesNoYesNo
Auto-numbering (IDENTITY / AUTO_INCREMENT)YesNoNoNo
DEFAULT valuesNoNoYesNo
Primary keyNoNoNoNo
IndexesNoNoNoNo
CHECK constraintsNoNoNoNo
Foreign keysNoNoNoNo
Triggers, permissionsNoNoNoNo

So the copy is a plain heap of rows. In PostgreSQL, \d orders_backup shows the types but nothing else, not even NOT NULL:

                     Table "public.orders_backup"
   Column    |         Type          | Collation | Nullable | Default
-------------+-----------------------+-----------+----------+---------
 id          | integer               |           |          |
 customer_id | integer               |           |          |
 status      | character varying(20) |           |          |
 amount      | numeric(10,2)         |           |          |
 order_date  | date                  |           |          | 

Which means the copy happily accepts rows the original would reject. This insert breaks the primary key (a second id 1), the foreign key (there is no customer 9), NOT NULL and the CHECK constraint (a negative amount), and PostgreSQL accepts it:

INSERT INTO orders_backup VALUES (1, 9, NULL, -5, '2026-12-01');

SELECT * FROM orders_backup WHERE id = 1;
 id | customer_id | status  | amount | order_date
----+-------------+---------+--------+------------
  1 |           1 | shipped | 120.00 | 2026-10-03
  1 |           9 |         |  -5.00 | 2026-12-01
(2 rows)

SQL Server is a little stricter because it keeps NOT NULL and IDENTITY, but a customer that doesn't exist and a negative amount still go straight in:

INSERT INTO orders_backup (customer_id, status, amount, order_date)
VALUES (9, 'x', -5, '2026-12-01');

SELECT * FROM orders_backup WHERE id = 7;
 id | customer_id | status | amount | order_date
----+-------------+--------+--------+------------
  7 |           9 | x      |  -5.00 | 2026-12-01

And because the default wasn't copied, leaving out status now fails instead of filling in 'pending':

Msg 515, Level 16, State 2
Cannot insert the value NULL into column 'status', table 'shop.dbo.orders_backup';
column does not allow nulls. INSERT fails.

For a throwaway backup or a report snapshot, none of this matters. If the copy is going to be a real table that people write to, add the keys and constraints back with ALTER TABLE:

ALTER TABLE orders_backup ADD PRIMARY KEY (id);
CREATE INDEX ix_orders_backup_date ON orders_backup(order_date);

Or create the table properly first (see CREATE TABLE) and fill it with INSERT INTO ... SELECT. PostgreSQL and MySQL can also clone the full definition with CREATE TABLE ... LIKE, shown below.

Copy the table structure without data

To get an empty table with the same columns, add a condition that is never true. The query returns no rows, so the table is created empty:

SELECT *
INTO orders_empty
FROM orders
WHERE 1 = 0;

SELECT COUNT(*) AS row_count FROM orders_empty;
(0 rows affected)

 row_count
-----------
         0

The same "what gets copied" rules apply: you get the columns and types, not the keys. SELECT TOP 0 * INTO orders_empty FROM orders does the same thing in SQL Server, and PostgreSQL also accepts CREATE TABLE orders_empty AS SELECT * FROM orders WITH NO DATA.

SELECT INTO from a JOIN or GROUP BY

The query can be anything: joins, GROUP BY, CASE, subqueries. That makes SELECT INTO handy for saving a report. One rule: every column needs a name. A calculated column without an alias fails in SQL Server:

SELECT customer_id, SUM(amount)
INTO totals
FROM orders
GROUP BY customer_id;
Msg 1038, Level 15, State 5
An object or column name is missing or empty. For SELECT INTO statements, verify each
column has a name. For other statements, look for empty alias names. Aliases defined
as "" or [] are not allowed. Change the alias to a valid name.

(PostgreSQL doesn't complain; it quietly names the column sum. MySQL names it SUM(amount), which you'd then have to quote every time. Neither is what you want.) Give every expression an alias with AS:

SELECT c.name,
       c.country,
       COUNT(*)      AS order_count,
       SUM(o.amount) AS total_spent
INTO customer_totals
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status <> 'cancelled'
GROUP BY c.name, c.country;

SELECT * FROM customer_totals ORDER BY total_spent DESC;
 name  | country | order_count | total_spent
-------+---------+-------------+-------------
 Chloe | France  |           1 |      210.00
 Ben   | USA     |           2 |      175.49
 Ana   | Spain   |           1 |      120.00
 Dev   | USA     |           1 |       15.00

The types come from the expressions, and some of them may surprise you. In SQL Server, total_spent became DECIMAL(38,2) (the widest decimal, because a SUM can grow) and both calculated columns allow NULL, while name and country kept VARCHAR and NOT NULL from customers. If you need a specific type, CAST the expression in the select list: CAST(SUM(o.amount) AS DECIMAL(12,2)) AS total_spent.

SELECT INTO a temp table

In SQL Server, a table name that starts with # is a temporary table: it's private to your session and dropped automatically when you disconnect. SELECT INTO #temp is probably the most common way to create one, because you don't need to declare the columns first:

SELECT *
INTO #recent_orders
FROM orders
WHERE order_date >= '2026-11-01';

SELECT * FROM #recent_orders;
(4 rows affected)

 id | customer_id | status    | amount | order_date
----+-------------+-----------+--------+------------
  3 |           1 | cancelled |  40.00 | 2026-11-02
  4 |           3 | shipped   | 210.00 | 2026-11-09
  5 |           2 | pending   |  99.99 | 2026-11-20
  6 |           4 | shipped   |  15.00 | 2026-11-28

Use ##name for a global temp table that other sessions can see. If you run the script twice in the same session, the second run fails because #recent_orders still exists; start the script with DROP TABLE IF EXISTS #recent_orders; (SQL Server 2016+).

Other databases spell it differently:

-- PostgreSQL
SELECT * INTO TEMP recent_orders FROM orders WHERE order_date >= '2026-11-01';
-- or
CREATE TEMP TABLE recent_orders AS SELECT * FROM orders WHERE order_date >= '2026-11-01';

-- MySQL
CREATE TEMPORARY TABLE recent_orders AS SELECT * FROM orders WHERE order_date >= '2026-11-01';

-- SQLite
CREATE TEMP TABLE recent_orders AS SELECT * FROM orders WHERE order_date >= '2026-11-01';

All three returned the same four rows. If you only need the intermediate result inside one query, a CTE avoids creating a table at all.

SELECT INTO an existing table (and the error you'll get)

SELECT INTO always creates the table. If a table with that name already exists, SQL Server refuses:

SELECT * INTO orders_backup FROM orders;
Msg 2714, Level 16, State 6
There is already an object named 'orders_backup' in the database.

PostgreSQL says ERROR: relation "orders_backup" already exists. There's no option to make SELECT INTO append. You have two choices, depending on what you want:

Add rows to the existing table: use INSERT INTO ... SELECT. List the columns on both sides, and leave out an identity column so the target numbers the new rows itself:

INSERT INTO orders_backup (customer_id, status, amount, order_date)
SELECT customer_id, status, amount, order_date
FROM orders
WHERE status = 'pending';
(1 rows affected)

Replace the table: drop it first, then run SELECT INTO again. This is the usual pattern in scripts that rebuild a reporting table:

DROP TABLE IF EXISTS orders_backup;

SELECT * INTO orders_backup FROM orders;
(6 rows affected)

Be sure you mean it: DROP TABLE deletes the old table and every row in it.

SELECT INTO vs INSERT INTO SELECT

These two look alike and both copy rows from a query, so they're easy to mix up. The difference is whether the target table exists yet:

SELECT ... INTO new_tableINSERT INTO table SELECT ...
Target tableMust not exist; it's createdMust already exist
Column typesTaken from the queryTaken from the target; values are converted
Keys, constraints, defaultsNot copied (see table above)The target's own rules are enforced
Run it twiceSecond run failsSecond run adds the rows again
Works inSQL Server, PostgreSQLEvery database
Typical useQuick backup, temp table, report snapshotLoading, archiving, appending to a table you designed

So which is better? For a one-off copy in SQL Server, SELECT INTO is shorter and usually faster (see performance). For anything permanent, create the table with the keys and constraints you want and fill it with INSERT INTO ... SELECT. It's more typing, but the table enforces its rules, and the same code runs on every database.

SELECT INTO and identity columns (SQL Server)

When you copy an IDENTITY column straight from one table, SQL Server keeps the identity property. That's why orders_backup numbered the extra row as 7 above, and why you can't insert your own id into it:

Msg 544, Level 16, State 1
Cannot insert explicit value for identity column in table 'orders_backup' when
IDENTITY_INSERT is set to OFF.

Three rules decide what happens:

  • Copying id from a single table keeps IDENTITY.
  • It's dropped if the query has a join or a UNION, or if the column is part of an expression. Copying o.id in a join of orders and customers produced a plain INT. To drop it on purpose, wrap the column in an expression: SELECT CAST(id AS INT) AS id, ... INTO ...
  • You can add a new numbered column with the IDENTITY() function, which only works in SELECT INTO:
SELECT IDENTITY(INT, 1, 1) AS row_id, name, country
INTO numbered_customers
FROM customers
WHERE country = 'USA';

SELECT * FROM numbered_customers;
 row_id | name | country
--------+------+---------
      1 | Ben  | USA
      2 | Dev  | USA

A table can only have one identity column, so adding IDENTITY() while also copying an identity column fails:

SELECT IDENTITY(INT, 1, 1) AS row_id, * INTO bad FROM orders;
Msg 8108, Level 16, State 1
Cannot add identity column, using the SELECT INTO statement, to table 'bad', which
already has column 'id' that inherits the identity property.

Fix it by dropping the identity from the old column with CAST(id AS INT) AS id, or by leaving id out. If you just want row numbers in the output of a query, rather than a stored column, ROW_NUMBER() is the tool for that.

SELECT INTO with ORDER BY and TOP

You can use ORDER BY in a SELECT INTO, and it's most useful together with TOP, where it decides which rows are copied:

SELECT TOP 2 *
INTO biggest_orders
FROM orders
ORDER BY amount DESC;

SELECT * FROM biggest_orders;
 id | customer_id | status  | amount | order_date
----+-------------+---------+--------+------------
  4 |           3 | shipped | 210.00 | 2026-11-09
  1 |           1 | shipped | 120.00 | 2026-10-03

What ORDER BY does not do is store the rows in that order. Tables have no built-in order, and Microsoft's documentation says the order of rows inserted by SELECT INTO isn't guaranteed. The output above happens to come back sorted, but don't rely on it: put an ORDER BY on the query that reads the table. (PostgreSQL, MySQL and SQLite use LIMIT instead of TOP.)

SELECT INTO another database (SQL Server)

The new table can live in another database on the same server. Use a three-part name, database.schema.table:

SELECT *
INTO archive.dbo.orders_2026
FROM shop.dbo.orders;
(6 rows affected)

The archive database must already exist, and you need permission to create tables in it. Since SQL Server 2017 you can also add ON filegroup_name after the table name to choose which filegroup the new table goes on.

SELECT INTO in PostgreSQL

Everything in the SQL Server sections above works the same way in PostgreSQL's plain SQL: SELECT * INTO new_table FROM ... creates the table (reporting SELECT 6), it fails if the table exists, and SELECT * INTO TEMP new_table makes a temp table. The PostgreSQL docs nevertheless recommend CREATE TABLE AS, for two reasons: it's the standard form, and SELECT INTO means something else inside PL/pgSQL.

CREATE TABLE orders_copy AS
SELECT * FROM orders;

-- don't fail if it's already there
CREATE TABLE IF NOT EXISTS orders_copy AS
SELECT * FROM orders;
NOTICE:  relation "orders_copy" already exists, skipping

Remember that neither form copies NOT NULL, defaults, identity or keys. To clone the full definition, use CREATE TABLE ... (LIKE ... INCLUDING ALL) and then copy the rows:

CREATE TABLE orders_like (LIKE orders INCLUDING ALL);
INSERT INTO orders_like OVERRIDING SYSTEM VALUE SELECT * FROM orders;
                                Table "public.orders_like"
   Column    |         Type          | Collation | Nullable |           Default
-------------+-----------------------+-----------+----------+------------------------------
 id          | integer               |           | not null | generated always as identity
 customer_id | integer               |           | not null |
 status      | character varying(20) |           | not null | 'pending'::character varying
 amount      | numeric(10,2)         |           | not null |
 order_date  | date                  |           | not null |
Indexes:
    "orders_like_pkey" PRIMARY KEY, btree (id)
Check constraints:
    "orders_amount_check" CHECK (amount >= 0::numeric)

That keeps NOT NULL, the default, the identity, the primary key and the CHECK. Foreign keys are never copied by LIKE; add them with ALTER TABLE. (OVERRIDING SYSTEM VALUE is only needed because id is GENERATED ALWAYS.)

SELECT INTO a variable in PL/pgSQL

Inside a PL/pgSQL function, procedure or DO block, SELECT ... INTO stores the result in a variable instead:

DO $$
DECLARE total NUMERIC;
BEGIN
  SELECT SUM(amount) INTO total FROM orders WHERE status = 'shipped';
  RAISE NOTICE 'Shipped total: %', total;
END $$;
NOTICE:  Shipped total: 420.50

If the query returns several rows, plain INTO silently keeps the first one. Add STRICT to get an error instead, which is usually what you want:

SELECT amount INTO STRICT total FROM orders WHERE status = 'shipped';
ERROR:  query returned more than one row
HINT:  Make sure the query returns a single row, or use LIMIT 1.

And trying to create a table with SELECT * INTO new_tbl FROM orders inside a DO block fails with "new_tbl" is not a known variable. Use CREATE TABLE new_tbl AS ... there.

SELECT INTO in MySQL

MySQL doesn't support SELECT INTO for creating tables. It reads the name after INTO as a variable, so the SQL Server syntax fails with a confusing message:

SELECT * INTO orders_backup FROM orders;
ERROR 1327 (42000): Undeclared variable: orders_backup

The MySQL way to create a table from a query is CREATE TABLE ... AS SELECT (the AS is optional in MySQL):

CREATE TABLE orders_backup AS
SELECT * FROM orders;

CREATE TABLE IF NOT EXISTS orders_backup AS
SELECT * FROM orders;   -- skipped with a warning if it exists

SHOW CREATE TABLE orders_backup shows MySQL keeps more than the others: NOT NULL and defaults survive, but AUTO_INCREMENT, the primary key, the index, the CHECK and the foreign key are gone. The old auto-increment column gets DEFAULT '0':

CREATE TABLE `orders_backup` (
  `id` int NOT NULL DEFAULT '0',
  `customer_id` int NOT NULL,
  `status` varchar(20) NOT NULL DEFAULT 'pending',
  `amount` decimal(10,2) NOT NULL,
  `order_date` date NOT NULL
) ENGINE=InnoDB ...

To keep everything except foreign keys, clone the definition with LIKE and copy the rows:

CREATE TABLE orders_like LIKE orders;
INSERT INTO orders_like SELECT * FROM orders;

SHOW CREATE TABLE orders_like then includes AUTO_INCREMENT, the PRIMARY KEY, both indexes and the CHECK constraint.

MySQL SELECT INTO a variable

What SELECT ... INTO does do in MySQL is store a single row's values in variables. INTO can go before FROM or at the end of the statement (the end is the position MySQL now recommends):

SELECT SUM(amount) INTO @shipped_total FROM orders WHERE status = 'shipped';
SELECT @shipped_total;

SELECT SUM(amount) FROM orders WHERE status = 'shipped' INTO @t2;
+----------------+
| @shipped_total |
+----------------+
|         420.50 |
+----------------+

The query must return at most one row. Otherwise: ERROR 1172 (42000): Result consisted of more than one row. Add a WHERE or LIMIT 1.

SELECT INTO OUTFILE

MySQL can also write query results to a file on the database server:

SELECT * FROM orders
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';
"1","1","shipped","120.00","2026-10-03"
"2","2","shipped","75.50","2026-10-15"
...

The file is written on the server, not your computer, and only into the directory named by the secure_file_priv setting (SHOW VARIABLES LIKE 'secure_file_priv'). Anywhere else gives ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement. You also need the FILE privilege, and the file must not already exist. To get a CSV on your own machine, a GUI client's export button is easier.

SELECT INTO in SQLite

SQLite has no SELECT INTO at all:

SELECT * INTO orders_backup FROM orders;
near "INTO": syntax error

Use CREATE TABLE ... AS SELECT, which works the same way, including CREATE TEMP TABLE ... AS and IF NOT EXISTS. Be aware that SQLite keeps even less than the others: the copy's columns get loose type names (the VARCHAR(20) column became TEXT, DECIMAL and DATE became NUM) and no constraints at all. Calculated columns without an alias get the expression as their name, such as "SUM(amount)".

Because the SQL Easy lessons run SQLite in your browser, this is the form you can try there right now:

The SQL Easy lesson editor running CREATE TABLE avid_readers AS SELECT name, species, num_books_read FROM family_members WHERE num_books_read > 190, then SELECT * FROM avid_readers, which returns one row: Dave, human, 200
Creating a table from a query in a SQL Easy lesson, then reading it back.

SELECT INTO in Oracle, Snowflake and BigQuery

  • Oracle: SELECT INTO exists only in PL/SQL, where it fetches exactly one row into variables (SELECT SUM(amount) INTO v_total FROM orders;). It raises NO_DATA_FOUND if no row comes back and TOO_MANY_ROWS if more than one does. Use BULK COLLECT INTO to fetch many rows into a collection. To create a table, use CREATE TABLE new_table AS SELECT ...; Oracle keeps NOT NULL constraints that were declared on the source columns but not keys or indexes. See the Oracle SELECT INTO reference.
  • Snowflake: create tables with CREATE TABLE new_table AS SELECT ... (or CREATE OR REPLACE TABLE ... AS to overwrite). In Snowflake Scripting blocks, SELECT ... INTO :variable assigns a value to a variable.
  • BigQuery: use CREATE TABLE dataset.new_table AS SELECT ... or CREATE OR REPLACE TABLE. There's no SELECT INTO; in scripts, assign variables with SET x = (SELECT ...).

Is SELECT INTO fast?

In SQL Server, usually yes, and that's one of the main reasons people use it. According to the Microsoft documentation:

  • Under the SIMPLE or BULK_LOGGED recovery model, SELECT INTO is minimally logged, so it writes far less to the transaction log than an equivalent CREATE TABLE + INSERT. Under the FULL recovery model it's fully logged.
  • Since SQL Server 2014 (compatibility level 110 and up), the insert part can run in parallel.
  • The new table has no indexes, which makes loading quick, but queries against a large copy will be slow until you add them. Create indexes after the data is loaded, not before.

INSERT INTO ... SELECT can be minimally logged too, if you add the WITH (TABLOCK) hint and the target is a heap, so the gap is smaller than it used to be. For a few thousand rows, none of this matters; pick the statement that says what you mean.

Common SELECT INTO errors

ErrorDatabaseCause and fix
There is already an object named 'x' in the database. (Msg 2714)SQL ServerThe target exists. Use INSERT INTO ... SELECT to append, or DROP TABLE IF EXISTS x first to replace it.
relation "x" already existsPostgreSQLSame cause. CREATE TABLE IF NOT EXISTS x AS ... skips it instead.
An object or column name is missing or empty... (Msg 1038)SQL ServerA calculated column has no name. Add AS alias to every expression.
Cannot add identity column, using the SELECT INTO statement... (Msg 8108)SQL ServerYou used IDENTITY() while copying an identity column. Use CAST(id AS INT) AS id or drop the column.
Incorrect syntax near the keyword 'INTO'. (Msg 156)SQL ServerINTO is in the wrong place. It goes after the select list, before FROM.
Undeclared variable: x (ERROR 1327)MySQLMySQL's SELECT INTO is for variables. Use CREATE TABLE x AS SELECT ....
Result consisted of more than one row (ERROR 1172)MySQLSELECT ... INTO @var returned several rows. Filter to one row or add LIMIT 1.
near "INTO": syntax errorSQLiteNot supported. Use CREATE TABLE x AS SELECT ....
"x" is not a known variablePostgreSQL (PL/pgSQL)Inside a function or DO block, INTO means a variable. Use CREATE TABLE x AS ....

FAQ

What does SELECT INTO do in SQL?

In SQL Server and PostgreSQL, SELECT ... INTO new_table FROM ... runs the query, creates a new table with matching columns, and inserts the results, all in one step. In MySQL, Oracle PL/SQL and PostgreSQL's PL/pgSQL, SELECT ... INTO stores a single row's values in variables instead.

Does SELECT INTO create a table?

Yes, in SQL Server and PostgreSQL it always creates a new table, and it fails if a table with that name already exists. It copies column names, types and data, but not primary keys, indexes, foreign keys, CHECK constraints or defaults.

Can I SELECT INTO an existing table?

No. To add rows to a table that already exists, use INSERT INTO existing_table (columns) SELECT columns FROM .... To replace it, run DROP TABLE IF EXISTS existing_table first and then SELECT INTO.

Which is better, SELECT INTO or INSERT INTO SELECT?

Use SELECT INTO for quick copies: backups, temp tables and report snapshots in SQL Server. Use INSERT INTO ... SELECT when the table already exists or will be a permanent table, because it keeps the target's keys and constraints and works in every database. See the comparison table.

What is INSERT INTO?

INSERT INTO adds new rows to an existing table, either from a list of values (INSERT INTO t (a, b) VALUES (1, 2)) or from a query (INSERT INTO t (a, b) SELECT ...). The INSERT INTO guide covers both.

Does SELECT INTO copy indexes and primary keys?

No database copies them. SQL Server keeps IDENTITY and NOT NULL; MySQL's CREATE TABLE AS keeps NOT NULL and defaults; PostgreSQL keeps only the types. Add keys afterwards with ALTER TABLE, or use CREATE TABLE ... LIKE in PostgreSQL and MySQL.

How do I copy only the table structure?

Add a condition that's never true: SELECT * INTO new_table FROM old_table WHERE 1 = 0; (or CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 = 0 elsewhere). You get the columns and types with no rows.

How do I do SELECT INTO in MySQL?

Use CREATE TABLE new_table AS SELECT ... FROM .... MySQL's own SELECT ... INTO only assigns variables (INTO @x) or writes files (INTO OUTFILE), and SELECT * INTO new_table fails with "Undeclared variable".

Is SELECT INTO DDL or DML?

Both, in effect: it creates a table (a DDL action) and inserts rows (a DML action) in one statement. That's why it needs CREATE TABLE permission in SQL Server as well as read access to the source. See DDL, DML, DQL and DCL.

Keep practicing

SELECT INTO is only as useful as the query in front of it, so the best way to get better at it is to get better at SELECT: filtering, joining and grouping. The free SQL Easy lessons walk you through all of that with exercises that run in your browser. Next, read about INSERT INTO for adding rows to tables you already have, and CREATE TABLE for designing tables with the keys and constraints that SELECT INTO leaves behind.