SQL INSERT INTO SELECT
The SQL INSERT INTO SELECT Statement used to copy data from one table and inserts it into another table
Syntax INSERT INTO SELECT
INSERT INTO table_2
SELECT * FROM table_1
WHERE [condition];
INSERT INTO SELECT Syntax [With COLUMNS]
INSERT INTO table_2 (column_1, column_2, column_3, ..)
SELECT (column_1, column_2, column_3, ..)
FROM table_1
WHERE [condition];
Query Statement Condition
- The data type of columns must be same in source and target tables.
- The target table existing records are unaffected.
- The INSERT INTO Statement can also contains many clauses including SELECT, GROUP BY, HAVING, JOIN, and ALIAS; For Example:
INSERT INTO Example with Cluse
INSERT INTO table_2 (column_1, column_2, column_3)
SELECT column_1, column_2, column_3
FROM table_1
GROUP BY column_1, column_3;