×

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help of examples.

The INSERT INTO SELECT query is used to add records from one table to another using the SELECT statement. In other words, we can say this statement copies data from one table and adds it to another.

Before using the INSERT INTO SELECT query, we shouldn't forget the key points as follows:

1 The data we will add to the table must have existed in the database from the table we will copy.

2 The data types must be the same as the source and target table.

The syntax of the INSERT INTO SELECT statement is as follows:

INSERT INTO Table_Name_1 (SELECT * FROM Table_Name_2 WHERE condition);

In the above syntax, the where a condition is optional. The WHERE clause is used to add selected data from one table to another.

Use the below syntax to add records into selected columns using the INSERT INTO SELECT query is as follows:

INSERT INTO Table_Name_2 (Column_Name1, Column_Name2, Column_Name3, Column_Name4) SELECT Column_Name1, Column_Name2, Column_Name3, Column_Name4 FROM Table_Name_2 WHERE Condition;

Let’s understand how to use the INSERT INTO SELECT statement with the help of an examples.

First, we will create a new table name Student_information:

CREATE TABLE Student_Information (Studn_info_Id int not null, Student_Name varchar(40) not null, Student_gender varchar(1) not null, Student_age int not null, Marks int not null, Degree varchar(40) not null, Primary key(Studn_info_Id));

Let’s add some data into the Student_information table:

INSERT INTO Student_Information values(1, 'Priya Chaudhary', 'F', 23, 560, 'BE');
INSERT INTO Student_Information values(2, 'Utkarsh Kulkarni', 'M', 23, 550, 'B.Tech');
INSERT INTO Student_Information values(3, 'Rakhi Jain', 'F', 22, 580, 'M.COM');
INSERT INTO Student_Information values(4, 'Nikita Ingale', 'F', 23, 620, 'BE');
INSERT INTO Student_Information values(5, 'Piyush Narkhede', 'M', 22, 600, 'BSC');
INSERT INTO Student_Information values(6, 'Pawan Sharma', 'M', 24, 590, 'B.COM');
INSERT INTO Student_Information values(7, 'Tushar Mahalle', 'M', 22, 680, 'B.Tech');
INSERT INTO Student_Information values(8, 'Sakashi Sharma', 'F', 21, 650, 'BSC');
INSERT INTO Student_Information values(9, 'Gaurav Gupta', 'M', 22, 635, 'BSC');
INSERT INTO Student_Information values(10, 'Manish Kapoor', 'M', 23, 500, 'MCOM');

We will execute the select query to display the records in the table form as follows:

SELECT * FROM Student_Information;
Studn_info_IdStudent_NameStudent_GenderStudent_ageMarksDegree
1Priya ChaudharyF23560BE
2Utkarsh KulkarniM23550B.Tech
3Rakhi JainF22580MCOM
4Nikita IngaleF23620BE
5Piyush NarkhedeM22600BSC
6Pawan SharmaM24590B.COM
7Tushar MahalleM22680B.Tech
8Sakashi SharmaF21650BSC
9Gaurav GuptaM22635B.COM
10Manish KapoorM23500MCOM

Now, we will create one more table named Student_Info which is our target table in this example.

CREATE TABLE Student_Info (Stud_info_Id int not null, Student_Name varchar(40) not null, Student_gender varchar(1) not null, Student_age int not null, Marks int not null, Degree varchar(40) not null, Primary key(Stud_info_Id));

Now, we will have another table name Student_Infos, which will be our second target table in this example.

CREATE TABLE Student_Infos (Stud_info_Id int not null, Student_Name varchar(40) not null, Student_gender varchar(1) not null, Student_age int not null, Marks int not null, Degree varchar(40) not null, Primary key(Stud_info_Id));

Example 1: Write a query to add all the records from the student_information table to the Student_info table using the INSERT INTO SELECT query.

INSERT INTO Student_Info (SELECT * FROM Student_Information);

In the above INSERT INTO SELECT query example, we copied the entire student_information table data into the Student_Info table.

We will execute the SELECT query on the Student_Info table to verify whether the data is successfully added or not as follows:

SELECT * FROM Student_Info;

The output of the above query is as follows:

Studn_info_IdStudent_NameStudent_GenderStudent_ageMarksDegree
1Priya ChaudharyF23560BE
2Utkarsh KulkarniM23550B.Tech
3Rakhi JainF22580MCOM
4Nikita IngaleF23620BE
5Piyush NarkhedeM22600BSC
6Pawan SharmaM24590B.COM
7Tushar MahalleM22680B.Tech
8Sakashi SharmaF21650BSC
9Gaurav GuptaM22635B.COM
10Manish KapoorM23500MCOM
SQL INSERT INTO SELECT

Example 2: Write a query to add all the records from the student_information table to the Student_infos table but only for selected columns using the INSERT INTO SELECT query.

INSERT INTO Student_Infos (Stud_Info_Id, Student_Name, Student_age, Marks, Degree) (SELECT Studn_Info_Id, Student_Name, Student_age, Marks, Degree FROM Student_Information); 

In the above INSERT INTO SELECT query example, we copied the selected column records from the student_information table data into the Student_Infos table.

We will execute the SELECT query on the Student_Info table to verify whether the data is successfully added or not as follows:

SELECT * FROM Student_Infos;

The output of the above query is as follows:

Studn_info_IdStudent_NameStudent_GenderStudent_ageMarksDegree
1Priya Chaudhary 23560BE
2Utkarsh Kulkarni 23550B.Tech
3Rakhi Jain 22580MCOM
4Nikita Ingale 23620BE
5Piyush Narkhede 22600BSC
6Pawan Sharma 24590B.COM
7Tushar Mahalle 22680B.Tech
8Sakashi Sharma 21650BSC
9Gaurav Gupta 22635B.COM
10Manish Kapoor 23500MCOM
SQL INSERT INTO SELECT

Now, we will have another table name Student_Infos, which will be our third target table in this example.

CREATE TABLE Students_Info (Stud_info_Id int not null, Student_Name varchar(40) not null, Student_gender varchar(1) not null, Student_age int not null, Marks int not null, Degree varchar(40) not null, Primary key(Stud_info_Id));

Example 3: Write a query to add the records from the student_information table to the Students_info table where student gender is ‘M’ using the INSERT INTO SELECT query.

INSERT INTO Students_Info (SELECT * FROM Student_Information WHERE Student_Gender = ‘M’);

In the above INSERT INTO SELECT query example, we copied the data from the student_information table where student gender is ‘M’ into the Students_Info table.

We will execute the SELECT query on the Students_Info table to verify whether the data is successfully added or not as follows:

SELECT * FROM Students_Info;

The output of the above query is as follows:

Studn_info_IdStudent_NameStudent_GenderStudent_ageMarksDegree
2Utkarsh KulkarniM23550B.Tech
5Piyush NarkhedeM22600BSC
6Pawan SharmaM24590B.COM
7Tushar MahalleM22680B.Tech
9Gaurav GuptaM22635B.COM
10Manish KapoorM23500MCOM
SQL INSERT INTO SELECT

Related Topics

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.