Description
The SQL keyword INSERT INTO SELECT is used to copy data from one table and insert it into another table.
- It requires the matching data types between the source and target tables.
- The existing records in the target table are unaffected.
- It can be used to copy data from all the columns or specific columns. In case a value is not specified for a specific column, such a column is filled with a NULL value.
INSERT INTO SELECT All Data
The below SQL copies all the data from the backup table to another existing table.
INSERT INTO customers
SELECT * FROM customers_backup2022;
INSERT INTO SELECT Specific Columns
The below SQL copies data from specific columns to another existing table.
INSERT INTO customers (customer_name, address, city, country, postal_code)
SELECT supplier_name, address, city, country, postal_code
FROM suppliers;
INSERT INTO SELECT Specific Rows
The below SQL copies data from specific rows to another existing table, using the WHERE condition to select rows.
INSERT INTO customers (customer_name, address, city, country, postal_code)
SELECT supplier_name, address, city, country, postal_code
FROM suppliers
WHERE country = 'Germany';