Populating Tables with SQL INERT INTO SELECT

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.

Why use INSERT INTO SELECT?

  • Efficiency: Instead of inserting rows one at a time, you can copy entire datasets in one operation.
  • Flexibility: You can select specific columns and rows from the source table.
  • Scalability: Works well with large data sets, making it an essential tool for data migration.

Basic Syntax and Usage

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.
  • The number of columns and their order in SELECT must match the INSERT INTO statement.

Copying All Data from One Table to Another

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:

Example:

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.

Output:

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.

Copying Specific Columns

Sometimes, you may not need to copy all columns, just a subset of them.

Example:

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.

Handling Data Types and Constraints

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.

Example:

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.

Using INSERT INTO SELECT with WHERE Clause

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.

Example:

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;

				
			

Output:

Only rows where the employee’s salary exceeds 50,000 will be inserted into the high_salary_employees table.

Populating Tables from Multiple Sources

You can even perform INSERT INTO SELECT from multiple tables using SQL joins to combine data before inserting it into the target table.

Example:

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;

				
			

Output:

The customer_orders table will now contain combined information from both the customers and orders tables.

Advanced Use Cases

Inserting Data with Aggregations:

You can also perform aggregate operations, such as summing or averaging data from the source table before inserting it into the target table.

Example:

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;

				
			

Output:

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 !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India