Inserting the result of a query in another table
All the rows or some rows of another table can also be inserted into the table using INSERT INTO statement. The rows of another table will be fetched based on one or more criteria using SQL SELECT statement.
Example:
Sample table: agents
Sample table: agentbangalore
To add records of 'agents' table into 'agentbangalore' table with the following condition -
1. the 'working_area' of 'agents' table must be 'Bangalore',
the following SQL statement can be used:
SQL Code:
-- This SQL code attempts to insert rows into the 'agentbangalore' table by selecting data from the 'agents' table based on a condition.
-- INSERT INTO statement begins
INSERT INTO agentbangalore
-- Specifies the target table 'agentbangalore' where the data will be inserted
SELECT * FROM agents
-- Selects all columns from the 'agents' table
WHERE working_area="Bangalore";
-- Filters the rows selected from the 'agents' table based on the condition that the 'working_area' column equals "Bangalore"
Explanation:
- This SQL code aims to insert rows into the 'agentbangalore' table.
- The INSERT INTO statement specifies the target table 'agentbangalore' where the data will be inserted.
- The SELECT statement is used to retrieve data from the 'agents' table.
- The '*' wildcard is used to select all columns from the 'agents' table.
- The WHERE clause filters the rows selected from the 'agents' table based on the condition that the 'working_area' column equals "Bangalore".
- Only the rows meeting this condition will be inserted into the 'agentbangalore' table.
Inserting the result of a query in another table with order by
An arranged order of rows (ascending or descending) of one table can also be inserted into another table by the use of SQL SELECT statement along with ORDER BY clause.
Example:
Sample table: agentbangalore
Sample table: agents
To add records of 'agents' table into 'agentbangalore' table with following conditions -
1. the rows of 'agents' table should be arranged in descending order on 'agent_name' column,
2. the 'working_area' of 'agents' table must be 'Bangalore',
the following SQL statement can be used:
SQL Code:
-- This SQL code attempts to insert sorted rows into the 'agentbangalore' table by selecting data from the 'agents' table based on a condition.
-- INSERT INTO statement begins
INSERT INTO agentbangalore
-- Specifies the target table 'agentbangalore' where the data will be inserted
SELECT * FROM agents
-- Selects all columns from the 'agents' table
WHERE working_area="Bangalore"
-- Filters the rows selected from the 'agents' table based on the condition that the 'working_area' column equals "Bangalore"
ORDER BY agent_name DESC;
-- Orders the selected rows in descending order based on the 'agent_name' column
Explanation:
- This SQL code aims to insert sorted rows into the 'agentbangalore' table.
- The INSERT INTO statement specifies the target table 'agentbangalore' where the data will be inserted.
- The SELECT statement is used to retrieve data from the 'agents' table.
- The '*' wildcard is used to select all columns from the 'agents' table.
- The WHERE clause filters the rows selected from the 'agents' table based on the condition that the 'working_area' column equals "Bangalore".
- Only the rows meeting this condition will be inserted into the 'agentbangalore' table.
- The ORDER BY clause orders the selected rows in descending order based on the 'agent_name' column.
- As a result, the sorted rows based on the 'agent_name' column will be inserted into the 'agentbangalore' table.
Inserting the result of a query in another table with group by
A group of rows of one table can also be inserted into another table by the use of SQL SELECT statement along with GROUP BY clause.
Example:
Sample table: orders
Sample table: daysorder
To add records into 'daysorder' table for the columns 'ord_date','ord_amount' and 'advance_amount' from the same columns of 'orders' table with following conditions -
1. the rows of 'orders' table should arranged into a group according to 'ord_date',
2. make a sum of 'ord_amount' for each group,
3. make a sum of 'advance_amount' for each group,
4. data of each group in 'orders' table should be inserted into the 'daysorder' table,
the following SQL statement can be used:
SQL Code:
-- This SQL code attempts to insert aggregated data into the 'daysorder' table by selecting and summarizing data from the 'orders' table.
-- INSERT INTO statement begins
INSERT INTO daysorder (ord_date,ord_amount,advance_amount)
-- Specifies the target table 'daysorder' and the columns where the data will be inserted
SELECT ord_date,SUM(ord_amount),SUM(advance_amount)
-- Selects the 'ord_date' column from the 'orders' table and calculates the sum of 'ord_amount' and 'advance_amount' for each 'ord_date'
FROM orders
-- Specifies the source table 'orders' from which the data will be selected
GROUP BY ord_date;
-- Groups the rows from the 'orders' table by 'ord_date' and calculates the sums for each group
Explanation:
- This SQL code aims to insert aggregated data into the 'daysorder' table.
- The INSERT INTO statement specifies the target table 'daysorder' and the columns where the data will be inserted: 'ord_date', 'ord_amount', and 'advance_amount'.
- The SELECT statement is used to retrieve data from the 'orders' table and perform aggregations.
- The 'ord_date' column is selected directly, and the SUM() function is used to calculate the sum of 'ord_amount' and 'advance_amount' for each 'ord_date'.
- The FROM clause specifies the source table 'orders' from which the data will be selected.
- The GROUP BY clause groups the rows from the 'orders' table by 'ord_date' so that the SUM() function calculates the sums for each group of rows with the same 'ord_date'.
- As a result, the aggregated data is inserted into the 'daysorder' table with one row for each distinct 'ord_date', along with the sum of 'ord_amount' and 'advance_amount' for that date.
Inserting records using select with group by and order by
In the following we are going to discuss, how records of another table can be inserted using SQL SELECT statement along with ORDER BY and GROUP BY in an INSERT INTO statement.
Example:
Sample table: orders
Sample table: daysorder
To insert records into 'daysorder' table for the columns 'ord_date','ord_amount' and 'advance_amount' from the same columns of 'orders' table with following conditions -
1. the rows of 'orders' table should be arranged into a group according to 'ord_date',
2. the rows of 'orders' table should be arranged in descending order on 'ord_date' column,
3. make a sum of 'ord_amount' for each group ,
4. make a sum of 'advance_amount' for each group ,
5. data of each group in 'orders' table should insert into the 'daysorder' table,
the following SQL statement can be used :
SQL Code:
-- This SQL code attempts to insert aggregated and sorted data into the 'daysorder' table by selecting and summarizing data from the 'orders' table.
-- INSERT INTO statement begins
INSERT INTO daysorder (ord_date,ord_amount,advance_amount)
-- Specifies the target table 'daysorder' and the columns where the data will be inserted
SELECT ord_date,SUM(ord_amount),SUM(advance_amount)
-- Selects the 'ord_date' column from the 'orders' table and calculates the sum of 'ord_amount' and 'advance_amount' for each 'ord_date'
FROM orders
-- Specifies the source table 'orders' from which the data will be selected
GROUP BY ord_date
-- Groups the rows from the 'orders' table by 'ord_date' and calculates the sums for each group
ORDER BY ord_date DESC
-- Orders the selected rows in descending order based on the 'ord_date' column
Explanation:
- This SQL code aims to insert aggregated and sorted data into the 'daysorder' table.
- The INSERT INTO statement specifies the target table 'daysorder' and the columns where the data will be inserted: 'ord_date', 'ord_amount', and 'advance_amount'.
- The SELECT statement is used to retrieve data from the 'orders' table and perform aggregations.
- The 'ord_date' column is selected directly, and the SUM() function is used to calculate the sum of 'ord_amount' and 'advance_amount' for each 'ord_date'.
- The FROM clause specifies the source table 'orders' from which the data will be selected.
- The GROUP BY clause groups the rows from the 'orders' table by 'ord_date' so that the SUM() function calculates the sums for each group of rows with the same 'ord_date'.
- The ORDER BY clause orders the selected rows in descending order based on the 'ord_date' column.
- As a result, the aggregated and sorted data is inserted into the 'daysorder' table, with one row for each distinct 'ord_date', along with the sum of 'ord_amount' and 'advance_amount' for that date.
Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: Insert null
Next: Insert using subqueries
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics