When working with databases, the ability to insert data into a table using a SELECT statement is a powerful feature that allows for the efficient transfer of data from one table to another, or even from one database to another. This technique is particularly useful in scenarios such as data migration, replication, or when creating backup tables. The basic syntax for inserting data into a table using a SELECT statement involves combining the INSERT INTO statement with a SELECT statement.
Basic Syntax and Usage

The basic syntax for this operation is as follows:
INSERT INTO target_table (column1, column2,...)
SELECT column1, column2,...
FROM source_table
[WHERE condition];
In this syntax, `target_table` is the table into which you want to insert the data, and `source_table` is the table from which you want to retrieve the data. The `column1`, `column2`, etc., in both the `INSERT INTO` and `SELECT` statements must match in terms of data type and should correspond to the columns you wish to populate in the target table.
Example Usage
Let’s consider an example to illustrate how this works. Suppose we have two tables, employees
and employee_archive
, with the following structures:
Table | Columns |
---|---|
employees | id, name, department, salary |
employee_archive | id, name, department, salary |

We want to archive all employees who are no longer active (let's assume there's an `active` column in the `employees` table that we haven't listed). The SQL statement to achieve this could look like the following:
INSERT INTO employee_archive (id, name, department, salary)
SELECT id, name, department, salary
FROM employees
WHERE active = 0;
This statement will insert all the rows from the `employees` table where the employee is not active into the `employee_archive` table.
Advanced Scenarios and Considerations

In more complex scenarios, you might need to transform the data before inserting it into the target table. This can be achieved by using various SQL functions within the SELECT
statement. For example, you might want to convert all names to uppercase before archiving:
INSERT INTO employee_archive (id, name, department, salary)
SELECT id, UPPER(name), department, salary
FROM employees
WHERE active = 0;
Additionally, you can use joins and subqueries within the `SELECT` statement to fetch data from multiple tables or based on more complex conditions.
Handling Auto-Incrementing IDs
A common issue encountered when inserting data using a SELECT
statement is how to handle auto-incrementing IDs. If the id
column in the target table is set to auto-increment, you typically do not want to specify a value for it in the INSERT
statement, allowing the database to generate the ID automatically. The approach to this varies between different database systems:
- MySQL and MariaDB: You can omit the auto-incrementing column from the
INSERT INTO
clause, and MySQL will automatically generate the ID.
INSERT INTO employee_archive (name, department, salary)
SELECT name, department, salary
FROM employees
WHERE active = 0;
- PostgreSQL: Similar to MySQL, you can omit the serial column (which is PostgreSQL’s auto-incrementing type) from the
INSERT
statement.
INSERT INTO employee_archive (name, department, salary)
SELECT name, department, salary
FROM employees
WHERE active = 0;
- Microsoft SQL Server: If the ID column is an IDENTITY column, you need to use the
SET IDENTITY_INSERT
statement to enable or disable the automatic generation of IDs, depending on your needs.
SET IDENTITY_INSERT employee_archive ON;
INSERT INTO employee_archive (id, name, department, salary)
SELECT id, name, department, salary
FROM employees
WHERE active = 0;
SET IDENTITY_INSERT employee_archive OFF;
Key Points
- Use the `INSERT INTO` statement combined with a `SELECT` statement to transfer data between tables.
- Ensure the data types of the columns in the `SELECT` statement match those in the `INSERT INTO` statement.
- Back up your database before performing bulk operations.
- Use SQL functions to transform data during the transfer process if necessary.
- Handle auto-incrementing IDs according to the specific database system you're using.
Inserting data into a table using a `SELECT` statement is a versatile and powerful technique in SQL, allowing for efficient data transfer and manipulation. By understanding how to apply this technique and considering the nuances of different database systems, you can effectively manage and maintain your databases.
What is the purpose of using INSERT INTO with a SELECT statement?
+The purpose is to transfer data from one table to another, which can be useful for data migration, replication, or creating backup tables.
How do I handle auto-incrementing IDs when using this technique?
+The approach varies by database system. For MySQL and PostgreSQL, you can omit the auto-incrementing column from the INSERT INTO clause. For Microsoft SQL Server, you may need to use SET IDENTITY_INSERT depending on your needs.
Can I transform data during the transfer process?
+Yes, you can use SQL functions within the SELECT statement to transform the data before inserting it into the target table.