Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

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.
[st_adsense]  

Syntax:

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT INTO t2
SELECT * FROM t1
WHERE condition;
INSERT INTO t2 SELECT * FROM t1 WHERE condition;
INSERT INTO t2
SELECT * FROM t1
WHERE condition;

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT INTO t2 (column1, column2, column3, …)
SELECT column1, column2, column3, …
FROM t1
WHERE condition;
INSERT INTO t2 (column1, column2, column3, …) SELECT column1, column2, column3, … FROM t1 WHERE condition;
 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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)
);
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) );
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)
 );
[st_adsense]  
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       | alex1@gmail.com |
|         102 |  Alex  |  0325145698  | 285 Java Lane Missouri  | alex2@gmail.com |
|         103 |  Alex  |  0452145874  | 188 Clay Street Indiana | alex3@gmail.com |
|         104 |  Emily |  0752645874  | 171 Jarvisville Road    | emily@gmail.com |
|         105 |  Jean  |  0052145874  | 188 Clay Street Indiana | jean@gmail.com  |
|         106 |  Bob   |  0152145874  | 285 Java Lane Missouri  | bob1@gmail.com  |
|         107 |  Bob   |  0952145874  | 819 Saint Francis Way   | bob2@gmail.com  |
+-------------+--------+--------------+-------------------------+-----------------+

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT INTO Suppliers (
name,
phone_number,
address,
country,
customer_id
)
SELECT
name,
phone_number,
address,
country,
customer_id
FROM
Customers;
INSERT INTO Suppliers ( name, phone_number, address, country, customer_id ) SELECT name, phone_number, address, country, customer_id FROM Customers;
 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT INTO Suppliers (
name,
phone_number,
address,
country,
customer_id
)
SELECT
name,
phone_number,
address,
email,
customer_id
FROM
Customers;
WHERE
country = 'US';
INSERT INTO Suppliers ( name, phone_number, address, country, customer_id ) SELECT name, phone_number, address, email, customer_id FROM Customers; WHERE country = 'US';
 INSERT INTO Suppliers (
     name, 
     phone_number, 
     address,
     country,
     customer_id
 )
 SELECT 
     name, 
     phone_number, 
     address,
     email,
     customer_id
 FROM 
     Customers;
 WHERE 
     country = 'US';
[st_adsense]  
After inserting data using INSERT INTO SELECT query, the following query returns the data from the “Suppliers” table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * FROM Suppliers;
SELECT * FROM Suppliers;
SELECT * FROM Suppliers;
> SELECT * FROM Suppliers;

+-------------+--------+--------------+-------------------------+-----------------+
| customer_id |  name  | phone_number |        address          |      email      |
+-------------+--------+--------------+-------------------------+-----------------+
|         101 |  Alex  |  0214564785  | 819 Saint Francis       | alex1@gmail.com |
|         102 |  Alex  |  0325145698  | 285 Java Lane Missouri  | alex2@gmail.com |
|         103 |  Alex  |  0452145874  | 188 Clay Street Indiana | alex3@gmail.com |
|         104 |  Emily |  0752645874  | 171 Jarvisville Road    | emily@gmail.com |
|         105 |  Jean  |  0052145874  | 188 Clay Street Indiana | jean@gmail.com  |
|         106 |  Bob   |  0152145874  | 285 Java Lane Missouri  | bob1@gmail.com  |
|         107 |  Bob   |  0952145874  | 819 Saint Francis Way   | bob2@gmail.com  |
+-------------+--------+--------------+-------------------------+-----------------+
mcq

Leave a Reply

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