We know how to insert a single row or an individual data in a table at a time, but if you want to insert multiple rows in a table. In addition to INSERT INTO you will combine it with the select statement.
In this command, you are using information from another table.
Let’s see the Syntax for sql insert into select:
- INSERT INTO “table 1” (“column1”, “column2”,….)
- SELECT “column3”, “column4”,….
- FROM “table2”;
The INSERT INTO statement can also contain many clauses like SELECT, GROUP BY, HAVING as well as JOIN and ALIAS. So the insert into select statement may be complicated some times.
Let?s take an example:
There is a table, named sales_data in a database name departmental store while table store_information contains the record of those data which are sold from the store per day. If you want to move data from sales_data to store_information
You should use the following syntax:
- INSERT INTO store (store_name, sales, transaction_date)
- SELECT store_name, sum (sales), transaction_date
- FROM sales_information
- GROUP BY store_name, transaction_date;