Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

INSERT INTO SELECT


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:

  1. INSERT INTO “table 1” (“column1”“column2”,….)
  2. SELECT “column3”“column4”,….
  3. 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:

  1. INSERT INTO store (store_name, sales, transaction_date)
  2. SELECT store_name, sum (sales), transaction_date
  3. FROM sales_information
  4. GROUP BY store_name, transaction_date;

Comments

comments