AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL INSERT INTO SELECT |
|
SQL > SQL Commands >
Insert Into Select Statement
The INSERT INTO SELECT statement is used to add multiple new records into a database table at one time. INSERT INTO SELECT lets you populate a table with many rows at once by using the result of a SELECT query as the data source—ideal for copying, transforming, or summarizing data between tables.
SyntaxThe syntax for INSERT INTO SELECT is as follows:
Note that this is the simplest form. The SELECT statement can easily contain WHERE, GROUP BY, and HAVING clauses, as well as table joins and aliases. Please note the number of columns specified in the SELECT statement must be the same as the number of columns specified in the INSERT INTO statement. ExampleWe use the following tables for our example. Table Store_Information
Table Sales_Data
Table Sales_Data has detailed sales information, while table Store_Information keeps summarized data on sales by store by day. To copy data from Sales_Data to Store_Information, we type in:
Please note that we specified the order of the columns to insert data into in the example above (the first column is Store_Name, the second column is Sales, and the third column is Txn_Date). While this is not absolutely necessary, it is a good practice to follow, as this can ensure that we are always inserting data into the correct column. ExercisesAssume the Sales_Data table above contains the following data:
1. Which of the following SQL statement is valid? (There may be more than one answer)
2. What data is inserted into the Store_Information table by the following
SQL statement?
3. Write a SQL statement that retrieves all sales data from the Sales_Data table and store total daily store sales data in the Store_Information table. How many rows are inserted?
Frequently Asked QuestionsQ: What does INSERT INTO SELECT do in SQL? Q: Does the number of columns in INSERT INTO have to match the SELECT? Q: Can INSERT INTO SELECT include WHERE, GROUP BY, or JOIN clauses? Q: What is the difference between INSERT INTO SELECT and SELECT INTO?
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.