Introduction
In this tutorial, I am going to explain the difference between INSERT INTO SELECT and SELECT INTO In SQL Server. This detailed article will cover the following topics,
- Introduction
- INSERT INTO SELECT Statement In SQL Server
- SELECT INTO Statement In SQL Server
- Difference Between INSERT INTO SELECT and SELECT INTO In SQL Server
- Conclusion
First, let's create a database with some tables containing some dummy data. Here, I am providing you with the database along with the tables containing the records, on which I am showing you the various examples. Let's see.
CREATE DATABASE OnkarSharma_Vatsa
PRINT 'New Database ''OnkarSharma_Vatsa'' Created'
GO
USE [OnkarSharma_Vatsa]
GO
CREATE TABLE [dbo].[Employee] (
EmployeeID INT IDENTITY (31100,1),
EmployerID BIGINT NOT NULL DEFAULT 228866,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(255) NOT NULL UNIQUE,
DepartmentID VARCHAR(100) NOT NULL,
Age INT NOT NULL,
GrossSalary BIGINT NOT NULL,
PerformanceBonus BIGINT,
ContactNo VARCHAR(25),
PRIMARY KEY (EmployeeID)
);
CREATE TABLE [dbo].[Restaurant](
RestaurantId INT IDENTITY (51, 1),
RestaurantName VARCHAR(MAX) NOT NULL,
Email VARCHAR(100),
City VARCHAR(100),
Zipcode VARCHAR(6) NOT NULL,
State VARCHAR(100) NOT NULL,
Country VARCHAR(100) NOT NULL,
PRIMARY KEY(RestaurantId)
);
CREATE TABLE [dbo].[tbl_Orders] (
OrderId INT IDENTITY (108, 1) PRIMARY KEY,
FoodieID INT,
OrderStatus TINYINT NOT NULL, -- ==>> OrderStatus: 4: Cancelled; 3: Pending; 2: Processing; 1: Completed
OrderDate DATE NOT NULL,
ShippedDate DATE,
RestaurantId INT NOT NULL,
);
CREATE TABLE [dbo].[tbl_OrderItems](
OrderId INT NOT NULL,
ItemId INT,
MenuId INT NOT NULL,
Quantity INT NOT NULL,
Price DECIMAL(6, 2) NOT NULL,
Discount DECIMAL(5, 2) NOT NULL DEFAULT 0,
PRIMARY KEY (ItemId)
);
CREATE TABLE [dbo].[tbl_Menu] (
MenuId INT IDENTITY (81, 1) PRIMARY KEY,
FoodCategoryID INT NOT NULL,
FoodName VARCHAR (255) NOT NULL,
TypeofFood VARCHAR (100) NOT NULL,
Price DECIMAL(6, 2) NOT NULL
);
Let's check our following tables by using the following queries.
1) To get the data from the "Employee" table, use the following query.
SELECT * FROM OnkarSharma_Vatsa..Employee

2) To get the data from the "Restaurant" table, use the following query.
SELECT * FROM OnkarSharma_Vatsa..Restaurant

INSERT INTO SELECT Statement In SQL Server
INSERT INTO SELECT statement in SQL Server is used to copy the data from the source table and insert it into the target table. But, before copying the data, the target table must exist in the database.
Key Points
- The INSERT INTO SELECT statement requires the target table to exist in the database before data can be transferred from the source table to the target table.
- The data types in the source and target tables must match for the INSERT INTO SELECT statement to work.
Syntax
INSERT [TOP (expression [PERCENT])]
INTO <target_table> (<column_name>)
SELECT * FROM <source_table>
[WHERE condition]
Note:
The TOP clause allows you to specify the number of rows returned by the query to insert into the target table. If you use the PERCENT option, the statement will insert a percentage of the rows instead.
Examples
The examples in this section demonstrate the functionality of the INSERT INTO SELECT Statement. Let's see.
1) Insert all data [all columns] from the source table to the destination table
Let's create a table named "[dbo].[Restaurant_backup_12112021]" for demonstration.
CREATE TABLE [dbo].[Restaurant_backup_12112021](
RestaurantId INT,
RestaurantName VARCHAR(MAX) NOT NULL,
Email VARCHAR(100),
City VARCHAR(100),
Zipcode VARCHAR(6) NOT NULL,
State VARCHAR(100) NOT NULL,
Country VARCHAR(100) NOT NULL,
);
The following query will insert all the addresses from "[dbo].[Restaurant]" table into the "[dbo].[Restaurant_backup_12112021]" table.
INSERT INTO [dbo].[Restaurant_backup_12112021]
SELECT *
FROM [dbo].[Restaurant]

To verify the insertion, use the following query.
SELECT * FROM [dbo].[Restaurant_backup_12112021]

2) Insert data of specific columns from the source table to the destination table
Let’s create a table named "[dbo].[tbl_Restaurant_Address]" for demonstration.
IF OBJECT_ID(N'[dbo].[tbl_Restaurant_Address]', N'U') IS NOT NULL
BEGIN
DROP TABLE [dbo].[tbl_Restaurant_Address]
END
CREATE TABLE [dbo].[tbl_Restaurant_Address](
AddressId INT IDENTITY (51, 1) PRIMARY KEY,
RestaurantName VARCHAR(MAX) NOT NULL,
Zipcode VARCHAR(6) NOT NULL,
State VARCHAR(100) NOT NULL,
Country VARCHAR(100) NOT NULL
);
The following query will insert all addresses from "[dbo].[Restaurant]" table into the "[dbo].[tbl_Restaurant_Address]" table.
INSERT INTO [dbo].[tbl_Restaurant_Address] (RestaurantName, Zipcode, State, Country)
SELECT RestaurantName, Zipcode, State, Country
FROM [dbo].[Restaurant]

To verify the insertion, use the following query.
SELECT * FROM [dbo].[tbl_Restaurant_Address]

3) Insert rows with WHERE condition
Let’s create a table named "tbl_Restaurant_Address" for demonstration.
IF OBJECT_ID(N'[dbo].[tbl_Restaurant_Address]', N'U') IS NOT NULL
BEGIN
DROP TABLE [dbo].[tbl_Restaurant_Address]
END
CREATE TABLE [dbo].[tbl_Restaurant_Address](
AddressId INT IDENTITY (51, 1) PRIMARY KEY,
RestaurantName VARCHAR(MAX) NOT NULL,
Zipcode VARCHAR(6) NOT NULL,
State VARCHAR(100) NOT NULL,
Country VARCHAR(100) NOT NULL
);
The following query will insert specific addresses from the "[dbo].[Restaurant]" table into the "[dbo].[tbl_Restaurant_Address]" table.
INSERT INTO [dbo].[tbl_Restaurant_Address] (RestaurantName, Zipcode, State, Country)
SELECT RestaurantName, Zipcode, State, Country
FROM [dbo].[Restaurant]
WHERE State IN ('UP', 'Delhi')

To verify the insertion, use the following query.
SELECT * FROM [dbo].[tbl_Restaurant_Address]

4) Insert top N of rows
A) Top N of rows
Let’s create a table named "tbl_Restaurant_Address" for demonstration.
IF OBJECT_ID(N'[dbo].[tbl_Restaurant_Address]', N'U') IS NOT NULL
BEGIN
DROP TABLE [dbo].[tbl_Restaurant_Address]
END
CREATE TABLE [dbo].[tbl_Restaurant_Address](
AddressId INT IDENTITY (51, 1) PRIMARY KEY,
RestaurantName VARCHAR(MAX) NOT NULL,
Zipcode VARCHAR(6) NOT NULL,
State VARCHAR(100) NOT NULL,
Country VARCHAR(100) NOT NULL
);
The following query will insert top 10 addresses from the "[dbo].[Restaurant]" table into the "[dbo].[tbl_Restaurant_Address]" table.
INSERT TOP (10)
INTO [dbo].[tbl_Restaurant_Address] (RestaurantName, Zipcode, State, Country)
SELECT RestaurantName, Zipcode, State, Country
FROM [dbo].[Restaurant]

To verify the insertion, use the following query.
SELECT * FROM [dbo].[tbl_Restaurant_Address]

B) Top N PERCENT of rows
Let’s create a table named "tbl_Restaurant_Address" for demonstration.
IF OBJECT_ID(N'[dbo].[tbl_Restaurant_Address]', N'U') IS NOT NULL
BEGIN
DROP TABLE [dbo].[tbl_Restaurant_Address]
END
CREATE TABLE [dbo].[tbl_Restaurant_Address](
AddressId INT IDENTITY (51, 1) PRIMARY KEY,
RestaurantName VARCHAR(MAX) NOT NULL,
Zipcode VARCHAR(6) NOT NULL,
State VARCHAR(100) NOT NULL,
Country VARCHAR(100) NOT NULL
);
The following query will insert top 10 PERCENT addresses from the "[dbo].[Restaurant]" table into the "[dbo].[tbl_Restaurant_Address]" table.
INSERT TOP (10) PERCENT
INTO [dbo].[tbl_Restaurant_Address] (RestaurantName, Zipcode, State, Country)
SELECT RestaurantName, Zipcode, State, Country
FROM [dbo].[Restaurant]

To verify the insertion, use the following query.
SELECT * FROM [dbo].[tbl_Restaurant_Address]

5) WITH JOIN Clause: To get data from multiple tables
Let’s create a table named "[dbo].[tbl_OrderDetails]" for demonstration.
IF OBJECT_ID(N'[dbo].[tbl_OrderDetails]', N'U') IS NOT NULL
BEGIN
DROP TABLE [dbo].[tbl_OrderDetails]
END
CREATE TABLE [dbo].[tbl_OrderDetails](
OrderDetailId INT IDENTITY (51, 1),
RestaurantName VARCHAR(MAX) NOT NULL,
Email VARCHAR(100),
City VARCHAR(100),
OrderStatus TINYINT NOT NULL, -- ==>> OrderStatus: 4: Cancelled; 3: Pending; 2: Processing; 1: Completed
OrderDate DATE NOT NULL,
ShippedDate DATE,
Quantity INT NOT NULL,
Price DECIMAL(6, 2) NOT NULL,
FoodName VARCHAR (255) NOT NULL
);
The following query will insert data from multiple tables into the "[dbo].[tbl_OrderDetails]" table.
















Join the conversation! Your thoughts help the community grow.