SQL Insert Into

Table Of Content:

  1. What Is SQL Insert Into Statement?
  2. Syntax Of SQL Insert Into Statement.
  3. Examples Of SQL Insert Into Statement.

(1) What Is SQL Insert Into Statement?

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

  • The INSERT INTO SELECT statement requires that the data types in the source and target tables match.

(2) Syntax Of SQL Insert Into Statement.

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;

(3) Examples Of SQL Insert Into Statement.

Customer Table:

Suppliers Table:

Example-1:

The following SQL statement copies “Suppliers” into “Customers” (the columns that are not filled with data, will contain NULL):
INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;

Example-2:

The following SQL statement copies “Suppliers” into “Customers” (fill all columns):
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
SELECT SupplierName, ContactName, Address, City, PostalCode, Country FROM Suppliers;

Example-3:

The following SQL statement copies only the German suppliers into “Customers”:
INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers
WHERE Country='Germany';

Leave a Reply

Your email address will not be published. Required fields are marked *