Code has been added to clipboard!

Using SQL SELECT INTO

Reading time 1 min
Published Aug 9, 2017
Updated Oct 9, 2019

TL;DR – You can use SQL SELECT INTO to duplicate information from one table to another. It can also produce a unique table which is then placed in a file group. The new table cannot be defined as a table variable.

The syntax for SQL SELECT INTO

Copying all columns to a table

Example
SELECT *
INTO ntable [IN edb]
FROM otable
WHERE condition;

Note: The names/types are described in SELECT. New names can be assigned using the AS clause.

Copying particular columns to a table

Example
SELECT column1, column2, column3, ...
INTO ntable [IN edb]
FROM otable
WHERE condition;

SQL SELECT INTO usage examples

Backing up a table

Example
SELECT * INTO CustomerBackup2018
FROM Customers;

Copying a table to other database

Example
SELECT Name, Contact INTO CustomerBackup2017
FROM Customers;

Only copying a few columns

Example
SELECT * INTO CustomerBackup2017 IN 'Backup.mdb'
FROM Customers;

Only copying entries from a single country

Example
SELECT * INTO CustomerGermany
FROM Customers
WHERE Country = 'Germany';

Copying multiple tables into a new one

Example
SELECT Customers.Name, Customer_orders.ID
INTO CustomerOrderBackup2017
FROM Customers
LEFT JOIN Order ON Customers.ID = Customer_orders.Customer_id;
Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL