Code has been added to clipboard!

SQL INSERT INTO SELECT

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

TL;DR – The SQL INSERT INTO SELECT statement can copy some or all columns from one table into another. The existing table rows are not affected.

Copying all columns to another table

Example
INSERT INTO mytable2 
SELECT * FROM mytable1 
WHERE condition;

Copying a few columns to another table

Example
INSERT INTO  mytable2 (column1, column2, column3, ...)
SELECT  column1, column2, column3, ...
FROM mytable1
WHERE condition;

Example using demo databases

The Developers table

ID Name City Country
1 Tom Kurkutis New York USA
2 Ana Fernandez London UK
3 Antonio Indigo Paris France
4 Aarav Kaelin Delhi India
5 Andrew Tumota Miami USA

The Customers table

ID Name Contact Address City Postal_Code Country
1 Ben Choplinks Ben Choplink Obeesre Str. 51 Rome 11207 Italy
2 Donald Rich Donald Richario Avda. de la Confgfstitución 4122 Tallin 17021 Estonia
3 Lilly Smilkins Lilly Smilkin Matadsderos 2312 Eguero 14023 Mexico
4 Brandinina Tom Hitchins 110 Hanegover Sq. London WB2 2DP UK
5 Carizmos Christiano Kerrys Berguvsesvägen 9 Luleå S-968 43 Sweden

Copying a few columns from Customers to Developers

Example
INSERT INTO Developers (Name, Country)
SELECT Name, Country FROM Customers;

Copying the Customers table into the Developers table

Example
INSERT INTO Developers (Name, City, Country)
SELECT Name, City, Country FROM Customers;

Copying only UK Customers into the Developers table

Example
INSERT INTO Developers (Name, City, Country)
SELECT Name, City, Country FROM Customers
WHERE Country='UK';
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