Get Started: SQL INSERT INTO SELECT Statement

In This Article, You Will Know About SQL INSERT INTO SELECT Statement. 

SQL INSERT INTO SELECT Statement – Before moving ahead, let’s know a bit about the SQL SELECT INTO Statement.

Table of Contents

SQL INSERT INTO SELECT Statement

The SQL INSERT INTO SELECT Statement is used to copy data from one table and insert it into another table.  

The SQL INSERT INTO SELECT Statement requires the data types in both tables should be the same.

Note: The existing data of the targeted table (where data is to be inserted) will not affect.  

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;
				
			

Example Database

Table name: Data_1

S.No

Name

Roll_No

Section_ID

1

Alex

5

A

2

Rolex

6

B

3

Salex

7

C

4

Jalex

8

C

5

Kalex

9

C

Table name: Data_2

S.No

Name

Roll_No

Section_ID

6

A

451

A

7

R

452

B

8

S

453

C

9

J

454

D

10

K

455

E

SQL INSERT INTO statement Example

Example: Use the SQL INSERT INTO statement to copy all data from Data_1 table to insert it into Data_2 table.

				
					INSERT INTO Data_2
SELECT *
FROM Data_1;

				
			

Example: Use the SQL INSERT INTO statement to copy a specific data from Data_1 table to insert it into Data_2 table.

				
					INSERT INTO Data_2 (Name, Roll_No)
SELECT Name, Roll_No
FROM Data_1;

				
			

Example: Use the SQL INSERT INTO statement to copy a specific data from Data_1 table to insert it into Data_2 table.

				
					INSERT INTO Data_2 (Name, Roll_No)
SELECT Name, Roll_No
FROM Data_1
WHERE Section_ID = 'C';

				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Leave a Comment

Stay in the loop

codingstreets