×

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

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 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.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute 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 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.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.