MySQL

MySQL INSERT INTO SELECT

The statement INSERT INTO SELECT copies data from one table and inserts it into another table.

  • The source and target table must have the same data type.
  • Existing records in the target table are not affected.
 

Syntax:

The following syntax, copy all columns from table t1 to table t2.

INSERT INTO t2
SELECT * FROM t1
WHERE condition;

The following syntax, copy only the selected columns from table t1 to table t2.

 INSERT INTO t2 (column1, column2, column3, …)
 SELECT column1, column2, column3, …
 FROM t1
 WHERE condition;

 

Example :

Suppose we have the following table “Suppliers” with the following structure.

CREATE TABLE Suppliers (
     id INT AUTO_INCREMENT,
     name VARCHAR(50) NOT NULL,
     phone_number VARCHAR(50),
     address VARCHAR(50),
     country VARCHAR(50),
     customer_id INT,
     PRIMARY KEY (id)
 );
 
Suppose we have the following “Customers” table (We will use it to fill the “Suppliers” table).

> SELECT * FROM Customers;

+-------------+--------+--------------+-------------------------+-----------------+
| customer_id |  name  | phone_number |        address          |      email      |
+-------------+--------+--------------+-------------------------+-----------------+
|         101 |  Alex  |  0214564785  | 819 Saint Francis       | [email protected] |
|         102 |  Alex  |  0325145698  | 285 Java Lane Missouri  | [email protected] |
|         103 |  Alex  |  0452145874  | 188 Clay Street Indiana | [email protected] |
|         104 |  Emily |  0752645874  | 171 Jarvisville Road    | [email protected] |
|         105 |  Jean  |  0052145874  | 188 Clay Street Indiana | [email protected]  |
|         106 |  Bob   |  0152145874  | 285 Java Lane Missouri  | [email protected]  |
|         107 |  Bob   |  0952145874  | 819 Saint Francis Way   | [email protected]  |
+-------------+--------+--------------+-------------------------+-----------------+

Now we will fill in the “Suppliers” table from the “Customers” table.

 INSERT INTO Suppliers (
     name, 
     phone_number, 
     address,
     country,
     customer_id
 )
 SELECT 
     name, 
     phone_number, 
     address,
     country,
     customer_id
 FROM 
     Customers;

The following query copies only the customers from ‘US’ into the “suppliers” table.

 INSERT INTO Suppliers (
     name, 
     phone_number, 
     address,
     country,
     customer_id
 )
 SELECT 
     name, 
     phone_number, 
     address,
     email,
     customer_id
 FROM 
     Customers;
 WHERE 
     country = 'US';
 
After inserting data using INSERT INTO SELECT query, the following query returns the data from the “Suppliers” table.

SELECT * FROM Suppliers;
> SELECT * FROM Suppliers;

+-------------+--------+--------------+-------------------------+-----------------+
| customer_id |  name  | phone_number |        address          |      email      |
+-------------+--------+--------------+-------------------------+-----------------+
|         101 |  Alex  |  0214564785  | 819 Saint Francis       | [email protected] |
|         102 |  Alex  |  0325145698  | 285 Java Lane Missouri  | [email protected] |
|         103 |  Alex  |  0452145874  | 188 Clay Street Indiana | [email protected] |
|         104 |  Emily |  0752645874  | 171 Jarvisville Road    | [email protected] |
|         105 |  Jean  |  0052145874  | 188 Clay Street Indiana | [email protected]  |
|         106 |  Bob   |  0152145874  | 285 Java Lane Missouri  | [email protected]  |
|         107 |  Bob   |  0952145874  | 819 Saint Francis Way   | [email protected]  |
+-------------+--------+--------------+-------------------------+-----------------+
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *