The INSERT INTO SELECT statement is one of the most efficient ways to populate tables in SQL by copying data from one table into another. This chapter will explore the INSERT INTO SELECT statement in detail, covering everything from the syntax to advanced use cases with multiple tables. By the end of this chapter, you will have a comprehensive understanding of how to use INSERT INTO SELECT to efficiently manage and migrate data within databases.
INSERT INTO SELECT
is a powerful SQL statement that allows users to transfer data from one table to another in bulk. This is particularly useful when you need to create backups, archive data, or combine data from multiple tables into a new structure.
INSERT INTO SELECT
?The basic syntax for INSERT INTO SELECT
looks like this:
INSERT INTO target_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table;
target_table
: The table where you want to insert the data.source_table
: The table from which data will be selected.SELECT
must match the INSERT INTO
statement.If you want to copy all data from one table to another without any filtering, this is the simplest form of the INSERT INTO SELECT
statement:
Assume you have two tables: employees_backup
(empty) and employees
(with data).
INSERT INTO employees_backup
SELECT *
FROM employees;
In this case, you are copying all columns and rows from the employees
table into the employees_backup
table.
If the employees
table has five rows, after running this query, the employees_backup
table will also contain five rows identical to the employees
table.
Sometimes, you may not need to copy all columns, just a subset of them.
You have a table called projects_backup
with only two columns (project_id
, project_name
), and you want to copy only these two columns from the projects
table.
INSERT INTO projects_backup (project_id, project_name)
SELECT project_id, project_name
FROM projects;
Here, only the selected columns project_id
and project_name
from the projects
table will be inserted into the projects_backup
table.
When copying data using INSERT INTO SELECT
, the data types of the source and target columns must be compatible. If there’s a mismatch, you can cast data types explicitly using the CAST()
function.
You want to copy data from a source_table
where a column employee_age
(of type VARCHAR) needs to be inserted into a target table where the corresponding column is an integer.
INSERT INTO target_table (employee_name, employee_age)
SELECT employee_name, CAST(employee_age AS INT)
FROM source_table;
This ensures that the employee_age
column is converted to an integer before being inserted.
You don’t always need to copy all data from a source table. By adding a WHERE
clause, you can filter rows before they’re inserted into the target table.
Suppose you want to copy only employees from the employees
table whose salary is greater than 50,000 into another table.
INSERT INTO high_salary_employees
SELECT *
FROM employees
WHERE salary > 50000;
Only rows where the employee’s salary exceeds 50,000 will be inserted into the high_salary_employees
table.
You can even perform INSERT INTO SELECT
from multiple tables using SQL joins to combine data before inserting it into the target table.
You have an orders
table and a customers
table. You want to populate a new table called customer_orders
with the combined data.
INSERT INTO customer_orders (customer_id, customer_name, order_id, order_date)
SELECT c.customer_id, c.customer_name, o.order_id, o.order_date
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;
The customer_orders
table will now contain combined information from both the customers
and orders
tables.
You can also perform aggregate operations, such as summing or averaging data from the source table before inserting it into the target table.
Insert aggregated sales data into a summary table:
INSERT INTO sales_summary (product_id, total_sales)
SELECT product_id, SUM(sales_amount)
FROM sales
GROUP BY product_id;
This query calculates the total sales for each product and inserts the summarized data into the sales_summary
table.
The INSERT INTO SELECT statement is an essential tool in SQL for efficiently transferring and manipulating data. From basic data migration between tables to advanced queries involving filtering, joining, and aggregating, this command allows for flexibility and scalability. By mastering the use of INSERT INTO SELECT, you will significantly enhance your SQL data management capabilities, ensuring both performance and accuracy in handling large datasets. Happy coding !❤️