TechnoLearnAcademy

TECHNOLEARN

SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement in SQL is used to insert data from one table into another. It allows you to copy data from one table or result set and insert it into a different table.
    • The data types of the source columns should match those of the destination table.
SYNTAX:
                       INSERT INTO destination_table(column1, column2 , column3 , ..)
                                    SELECT column1 , column2 , column3, …
                                              FROM source_table
                                                 WHERE condition;
Let’s understand the select into statement with an example. For demonstration purposes, we will be using the table emp
         Table EMP
Example:
   Objective: In this example, The data from the emp table will be inserted into the customer table.
                      INSERT INTO Tbl_Customer(Ecode, FirstName, LastName, Emailaddress)                                                                                      SELECT Ecode, FirstName, LastName, Emailaddress FROM Tbl_Emp                    
    Query:
                      SELECT * FROM Tbl_Customer;
Example:
   Objective: In this example, data from the emp table will be inserted into the customer table based on specific conditions.
                      INSERT INTO Tbl_Customer(Ecode, FirstName, LastName, Emailaddress)                                                                                      SELECT Ecode, FirstName, LastName, Emailaddress FROM Tbl_Emp                                                                                                                       WHERE Department = ‘IT’
    Query:
                      SELECT * FROM Tbl_Customer;
Scroll to Top