SQL INSERT INTO SELECT

The SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another existing table.

This statement requires that the data types in the source and target tables match.


INSERT INTO SELECT Syntax

Copy all columns from one table to another table:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Demo Database

Customers Table (before insert):

CustomerName Country
Alfreds Futterkiste Germany
Ana Trujillo Emparedados Mexico

Suppliers Table:

SupplierName Country
Exotic Liquids UK
New Orleans Cajun Delights USA

INSERT INTO SELECT Example

The following SQL statement copies the names and countries from "Suppliers" into "Customers":

INSERT INTO SELECT Example

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;

This query takes the SupplierName and Country from the Suppliers table and inserts them as new rows into the CustomerName and Country columns of the Customers table.