HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

The SQL INSERT INTO SELECT 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 source and target tables match.

Note: The existing records in the target table are unaffected.

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 Employee table


This employee table is used for examples:

demo table

Demo Supplier table


This supplier table is used for examples:

demo suplier table

SQL INSERT INTO SELECT Examples


Example

Copy "Suppliers" into "employee" (the columns that are not filled with data, will contain NULL):

INSERT INTO employee (Employee_name, City, Country)
SELECT Supplier_name, City, Country FROM supplier ;
You can click on above box to edit the code and run again.

Example

Copy "supplier" into "employee" (fill all columns):
              
 INSERT INTO  employee (Employee_name, Address, City, Pincode, Country)
              
 SELECT Supplier_name, Address, City, Pincode, Country
 FROM  supplier  
;
You can click on above box to edit the code and run again.

Example

Copy only the Indian suppliers into "employee":
              
 INSERT INTO  employee (Employee_name, City, Country)
              
 SELECT Supplier_name, City, Country
 FROM  supplier 
 WHERE  Country= 'India'  
;
You can click on above box to edit the code and run again.

Output