WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL INSERT INTO SELECT Statement


With SQL, you can copy information from one table into another.

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


The SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement selects data from one table and inserts it into an existing table. Any existing rows in the target table are unaffected.

SQL INSERT INTO SELECT Syntax

We can copy all columns from one table to another, existing table:

INSERT INTO demo2
SELECT * FROM demo1;

Or we can copy only the columns we want to into another, existing table:

INSERT INTO demo2
(column_name(s))
SELECT column_name(s)
FROM demo1;

Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

CustomerID CustomerName ContactName Address City PostalCode Country
1

Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico

And a selection from the "Distributor" table:

SupplierID DistributorName UserName Address City Postal Code Country Phone
1 Hildigunn Jensdóttir Hadval61 Conaway Street Ferdinand IN 47532 USA 812-367-6449
2 Aimee Roberts Cogy1956 Valley Street Camden NJ 08102 USA 856-756-7383
3 Jay Hill Regina Murphy Spruce Drive Scenery Hill PA 15360 USA 724-945-0728

SQL INSERT INTO SELECT Examples

Copy only a few columns from "Distrubutor" into "Customers":

Example

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

Copy only the German Distrubutor into "Customers":

Example

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Distrubutor
WHERE Country='USA';