Back to all posts

SQL SELECT INTO Statement Explained with Examples

For SQL beginners who can write a SELECT and want to keep its rows in a new table.

On this page

SELECT INTO creates a new table and fills it with the rows that a query returns. For example, you can copy a students table into a backup table before you change it.

SQL Server and PostgreSQL run SELECT INTO. MySQL, Oracle and SQLite create the same table with another statement, CREATE TABLE AS.

SELECT column1, column2, ...
INTO new_table
FROM source_table
WHERE condition;
  • column1, column2, ... become the columns of new_table, with their names and data types. * copies every column.
  • INTO new_table names the table to create. No table may have that name yet.
  • FROM source_table is where the rows come from. It can be a join of several tables.
  • WHERE condition picks the rows to copy. Without WHERE, every row is copied.

Create a sample table of three students:

CREATE TABLE students (
  id   int PRIMARY KEY,
  name varchar(50) NOT NULL,
  age  int
);

INSERT INTO students VALUES
  (1, 'Alice',   20),
  (2, 'Bob',     22),
  (3, 'Charlie', 21);

Run the examples in order. Some later statements read the tables that earlier ones create.

Every statement was run on PostgreSQL 18. The SQL Server statements are the same, and were checked against Microsoft's documentation only.

SELECT INTO

SELECT * INTO copies every column and every row of a table into a new table. For example, you can keep a copy of students before a bulk update.

SELECT *
INTO backup_students
FROM students;

SELECT * FROM backup_students returns:

idnameage
1Alice20
2Bob22
3Charlie21

backup_students has the three columns and the three rows of students. SELECT INTO returns no rows itself, so query the new table to see the copy. Updating students after the copy doesn't change backup_students.

The primary key of students isn't copied:

SELECT * INTO backup_students FROM students: SELECT INTO creates the table, then inserts the rows. The column names, data types and three rows are copied; the primary key, indexes, constraints and triggers are left behind. NOT NULL is kept by SQL Server and dropped by PostgreSQL.

Columns and AS

A column list names the columns to copy. An alias written with AS gives a column a new name in the new table. For example, you can copy each student's id and name, with name renamed to student_name.

SELECT id, name AS student_name
INTO student_names
FROM students;

SELECT * FROM student_names returns:

idstudent_name
1Alice
2Bob
3Charlie

In student_names, the column student_name holds the values of name. age isn't in the column list, so student_names has no age column.

SELECT INTO with WHERE

A WHERE clause copies only the rows that match its condition. For example, you can copy the students older than 20.

SELECT *
INTO older_students
FROM students
WHERE age > 20;

SELECT * FROM older_students returns:

idnameage
2Bob22
3Charlie21

Bob is 22 and Charlie is 21, so both are copied. Alice is 20, which isn't greater than 20, so her row stays in students only.

WHERE 1 = 0 for an empty copy

With a condition that is false for every row, SELECT INTO creates the table with no rows. For example, you can create an empty table with the columns of students.

SELECT *
INTO empty_students
FROM students
WHERE 1 = 0;

SELECT * FROM empty_students returns the three columns and no rows:

idnameage

1 = 0 is false for every row, so no row is copied. The columns come from students, with their data types.

SELECT INTO with JOIN

SELECT INTO can copy the rows of a join of several tables. For example, you can copy each student with the course that the student takes.

Create a second table, courses, with a course for Alice and one for Bob:

CREATE TABLE courses (
  student_id  int,
  course_name varchar(50)
);

INSERT INTO courses VALUES
  (1, 'Math'),
  (2, 'History');

The SELECT INTO joins students to courses:

SELECT students.id, students.name, courses.course_name
INTO student_courses
FROM students
INNER JOIN courses ON courses.student_id = students.id;

SELECT * FROM student_courses returns:

idnamecourse_name
1AliceMath
2BobHistory

Alice and Bob each have a course, so the copy has a row for each of them. INNER JOIN keeps only the students with a match in courses, so Charlie isn't copied.

CREATE TABLE AS

CREATE TABLE AS creates a table from a query, as SELECT INTO does. For example, you can copy students into students_copy on MySQL.

CREATE TABLE students_copy AS
SELECT * FROM students;

SELECT * FROM students_copy returns the same rows on PostgreSQL 18, MySQL 9.1 and SQLite 3.50:

idnameage
1Alice20
2Bob22
3Charlie21

students_copy got the three columns and the three rows of students, as backup_students did. Which statement to use depends on the engine:

EngineSELECT INTO creates a tableStatement to use
SQL ServerYesSELECT ... INTO
PostgreSQLYesCREATE TABLE ... AS SELECT, or SELECT ... INTO
MySQLNoCREATE TABLE ... AS SELECT
OracleNoCREATE TABLE ... AS SELECT
SQLiteNoCREATE TABLE ... AS SELECT

PostgreSQL recommends CREATE TABLE AS, because SELECT INTO means something else inside functions written in PL/pgSQL, its procedural language. In MySQL, SELECT INTO stores values in variables or writes them to a file. MySQL rejects SELECT * INTO backup_students FROM students with "Undeclared variable: backup_students".

In Oracle, SELECT INTO is a PL/SQL statement that stores values in variables. SQLite has no SELECT INTO, and reports a syntax error near "INTO". Oracle wasn't run for this article: its CREATE TABLE reference documents CREATE TABLE ... AS SELECT.

What goes wrong

The table already exists

PostgreSQL rejects the first example when you run it a second time:

SELECT *
INTO backup_students
FROM students;
ERROR:  relation "backup_students" already exists

The statement fails, and backup_students keeps its three rows. To add rows to a table that exists, use INSERT INTO SELECT.

The copy has no primary key

SELECT INTO copies no primary key, index, constraint or trigger, as Microsoft documents for SQL Server. On PostgreSQL, backup_students accepts a second row with id 1:

INSERT INTO backup_students VALUES (1, 'Alice', 20);

SELECT * FROM backup_students now returns four rows:

idnameage
1Alice20
2Bob22
3Charlie21
1Alice20

students rejects the same INSERT, because id is its primary key. Add the keys that the copy needs before anything writes to it. For example, give older_students its primary key:

ALTER TABLE older_students ADD PRIMARY KEY (id);

NOT NULL depends on the engine. SQL Server keeps each column's NOT NULL. PostgreSQL drops NOT NULL, so backup_students also accepts a row with no name.

An expression without a name

Name every expression with AS, such as age + 1 AS next_age. The new table takes its column names from the columns after SELECT. PostgreSQL makes up a name for an expression without an alias:

SELECT name, age + 1
INTO next_year
FROM students;

SELECT * FROM next_year returns:

name?column?
Alice21
Bob23
Charlie22

The second column is called ?column?. Every query on next_year has to put that name in double quotes.

SQL Server rejects the statement with error 1038: "An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name."

A failed copy leaves an empty table on SQL Server

SQL Server creates the table first and inserts the rows second. If the inserts fail, the inserted rows roll back and the empty table remains. The next attempt then fails on the name.

Drop the empty table, or run SELECT INTO inside an explicit transaction. On PostgreSQL, a SELECT INTO that fails on a division by zero leaves no table behind.

Run SELECT INTO in DbSchema

The SQL Editor in DbSchema runs SQL in the connected database. Check the rows before SELECT INTO creates the table:

  1. Press Editor in the toolbar and choose SQL Editor.
  2. Type the query without its INTO line, press Run, and read the rows in the result grid.
  3. Add the INTO line, and press Run again.
The SELECT of the students older than 20 in the DbSchema SQL Editor, with Bob and Charlie in the result grid, before the INTO line is added

SELECT INTO creates the table in the connected database as soon as you press Run.

Download DbSchema at https://dbschema.com/download.html and connect it to PostgreSQL or SQL Server. Paste the sample table's CREATE TABLE and INSERT statements into the SQL Editor and press Run Script, then run each SELECT INTO and query the new table. The free Community Edition includes the database connection and the SQL Editor.

Run your SELECT INTO against a real schema

DbSchema reverse-engineers your database into an interactive diagram and runs SQL against it. The SQL editor is in the free Community Edition.