What are the differences and uses of SELECT INTO and INSERT INTO SELECT?

“SELECT INTO” and “INSERT INTO SELECT” are two different SQL statements used in relational databases to copy data from one table to another.

  1. SELECT INTO statement is used to create a new table and copy data from the source table into the new table. The syntax is as follows:
    SELECT * INTO new_table FROM old_table WHERE condition;
    In this statement, new_table is the name of the new table to be created, old_table is the name of the source table, and condition is an optional filtering condition. This statement will create a new table with the same structure as old_table and copy the data that meets the condition into the new table.
  2. The INSERT INTO SELECT statement is used to insert data from a source table into an existing target table. The syntax is as follows:
    INSERT INTO target_table(column1, column2, …) SELECT column1, column2, … FROM source_table WHERE condition;
    In this statement, target_table is the name of the target table, source_table is the name of the source table, and condition is an optional filtering condition. This statement will insert the source table data that meets the condition into the specified columns of the target table.

Summary:
SELECT INTO is used to create a new table and copy data from the source table that meets the conditions into the new table;
INSERT INTO SELECT is used to insert data from the source table that meets the conditions into an existing target table.

bannerAds