The INSERT INTO ... SELECT statement is used to insert data into a table by selecting it from one or more other tables.
1.Basic Syntax
- target_table → Table you want to insert into
- source_table → Table you are selecting data from
- Columns must match in number and compatible data types
2.Example Tables
Employees_2024
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 1 | John | IT | 5000 |
| 2 | Jane | HR | 4500 |
| 3 | Mike | IT | 6000 |
Employees_Archive (empty)
| EmployeeID | Name | Department | Salary |
|---|
3.Insert Data from Another Table
Result:
Employees_Archive
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 1 | John | IT | 5000 |
| 3 | Mike | IT | 6000 |
Only IT department employees are copied.
4.Insert Data from a Join
- Can aggregate data while inserting into another table
5.Insert Data with Transformations
- Can modify or calculate columns during insert
6.Insert from Multiple Tables (Using Joins)
- Combines multiple source tables and inserts into target table
7.Insert Using SELECT Without Specifying Columns
⚠ Must match all columns and order exactly.
8.Identity Column Consideration
If target table has an IDENTITY column:
