WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL INSERT INTO SELECT Statement


With SQL, you can duplicate data from one table into another.

The INSERT INTO SELECT articulation duplicates information from one table and embeds it into a current table.


The SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT articulation chooses information from one table and embeds it into a current table. Any current columns in the objective table are unaffected.

SQL INSERT INTO SELECT Syntax

We can duplicate all segments starting with one table then onto the next, existing table:

Supplement INTO demo2
SELECT * FROM demo1;

Or we can duplicate just the segments we need to into another, current table:

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

Database

In this instructional exercise we will utilize the outstanding Northwind test database.

Below is a determination from the "Clients" 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 choice from the "Wholesaler" 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 View Hill PA 15360 USA 724-945-0728

SQL INSERT INTO SELECT Examples

Copy just a couple of sections from "Distrubutor" into "Customers":

Example

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

Copy just the German Distrubutor into "Customers":

Example

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