Insert Into Select statement in MS SQL Server
The INSERT INTO SELECT
statement in SQL Server is a versatile feature that enables you to efficiently copy data from one or more tables into another table. This functionality is essential for tasks such as data transfer, backup creation, and data merging.
In this article, We will learn to Insert Into Select statement in MS SQL Server by understanding various examples on detail and so on.
SQL Server INSERT INTO SELECT
- The
INSERT INTO SELECT
statement in SQL Server is a powerful feature that allows us to copy data from one or more tables into another table. - This is useful for transferring data between tables, creating backups or merging datasets.
- We can insert all rows specific rows based on conditions, or even a subset of rows using filters such as
TOP
or percentage limits.
INSERT INTO target_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;
Explanation:
target_table
: The table into which the data will be inserted.source_table
: The table from which data is selected.condition
(optional): Filters to specify which rows to select.
Examples of SQL Server INSERT INTO SELECT
Example 1: Insert All Rows from Another Table
Suppose we need to archive employee data by inserting all employee records from the Employees
table into the EmployeesArchive
table using a single query.
INSERT INTO EmployeesArchive (EmployeeID, FirstName, LastName)
SELECT EmployeeID, FirstName, LastName
FROM Employees;
Source Table (Employees
):
EmployeeID | FirstName | LastName |
---|---|---|
1 | John | Doe |
2 | Jane | Smith |
3 | Bob | Johnson |
Output (EmployeesArchive
after insertion):
EmployeeID | FirstName | LastName |
---|---|---|
1 | John | Doe |
2 | Jane | Smith |
3 | Bob | Johnson |
Explanation: This query copies all rows from the Employees
table to the EmployeesArchive
table. Every row from Employees
will be inserted into the target table.
Example 2: Insert Some Rows from Another Table
Supose we need to insert employee records from the Employees
table into the EmployeesArchive
table for employees hired before January 1, 2020.
INSERT INTO EmployeesArchive (EmployeeID, FirstName, LastName)
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE HireDate < '2020-01-01';
Source Table (Employees
):
EmployeeID | FirstName | LastName | HireDate |
---|---|---|---|
1 | John | Doe | 2019-12-15 |
2 | Jane | Smith | 2020-05-10 |
3 | Bob | Johnson | 2018-03-22 |
Output (EmployeesArchive
after insertion):
EmployeeID | FirstName | LastName |
---|---|---|
1 | John | Doe |
3 | Bob | Johnson |
Explanation: This query inserts only the rows from the Employees
table where the HireDate
is before January 1, 2020, into the EmployeesArchive
table.
Example 3: Insert the Top N Rows
Suppose we need to insert the top 5 earliest hired employees from the Employees
table into the EmployeesArchive
table to create a snapshot of the most senior employees based on their hire date.
INSERT INTO EmployeesArchive (EmployeeID, FirstName, LastName)
SELECT TOP 5 EmployeeID, FirstName, LastName
FROM Employees
ORDER BY HireDate ASC;
Source Table (Employees
):
EmployeeID | FirstName | LastName | HireDate |
---|---|---|---|
1 | John | Doe | 2019-12-15 |
2 | Jane | Smith | 2020-05-10 |
3 | Bob | Johnson | 2018-03-22 |
Output (EmployeesArchive
after insertion):
EmployeeID | FirstName | LastName |
---|---|---|
3 | Bob | Johnson |
1 | John | Doe |
Explanation: This query selects and inserts the top 5 rows (earliest hired employees) from the Employees
table into the EmployeesArchive
table, based on HireDate
.
Example 4: Insert the Top Percent of Rows
Suppose we need to insert the top 10% of employees, based on their hire date, from the Employees
table into the EmployeesArchive
table to archive the most senior employees.
INSERT INTO EmployeesArchive (EmployeeID, FirstName, LastName)
SELECT TOP 10 PERCENT EmployeeID, FirstName, LastName
FROM Employees
ORDER BY HireDate ASC;
Source Table (Employees
):
EmployeeID | FirstName | LastName | HireDate |
---|---|---|---|
1 | John | Doe | 2019-12-15 |
2 | Jane | Smith | 2020-05-10 |
3 | Bob | Johnson | 2018-03-22 |
Output (EmployeesArchive
after insertion):
EmployeeID | FirstName | LastName |
---|---|---|
3 | Bob | Johnson |
Explanation: This query inserts the top 10% of employees from the Employees
table into the EmployeesArchive
, based on their HireDate
.
Conclusion
The INSERT INTO SELECT
statement is a flexible and efficient way to transfer data between tables in SQL Server. It supports a wide range of use cases, including full table copying, conditional insertion, and even limiting the number of rows inserted. This versatility makes it an essential tool in data migration and management tasks.