SQL Server INSERT Multiple Rows
Last Updated :
12 Dec, 2023
SQL Server is a relational Database Management System(RDBMS). It offers various features for creating, and managing databases with its efficient tools. It can handle both small-scale and large-scale industry database applications.
INSERT Statement in SQL Server
The Insert statement is a command of the Data Manipulation Language (DML) of DBMS. After the creation of the table, we use this INSERT statement to insert the rows in the table. In this article let us see the ways to insert the rows into the table.
If we want to insert all the columns of the table in the form of the row with the ', ' separator we need not explicitly mention all the column names by default all the columns will be considered.
Syntax:
INSERT INTO tableName
VALUES (row1),
(row2),
(row3);
Explanation: The values in the rows should be comma-separated.
If we want to insert only particular columns of the table in the form of rows ' , ' separated then we need to explicitly mention the column names of the column with ' , ' separated that we want to add to the table.
Process to Insert Multiple Rows in the Table
To understand the INSERT MULTIPLE Rows in the Table, we need a table for performing the operation. So here we have coursesActive Table which consists of courseId, courseName, courseCost and, studentsEnrolled Columns. If you don't know How to Create a Table in SQL Server then Refer to this.
CREATE TABLE CoursesActive
(
courseId INT PRIMARY KEY,
courseName VARCHAR(100),
courseCost INT,
studentsEnrolled BIGINT
);
Let's Insert some data into the coursesActive Table.
INSERT INTO CoursesActive
VALUES (1001, 'Data Structures And Algorithms', 4050, 20000000),
(1002, 'Java for Beginners',1215,100000),
(1003, 'Java Advanced',2435,200000),
(1004, 'DBMS',1620,3000000),
(1005, 'Operating Systems',1458, 200000),
(1006, 'Python', 2000, 1500000),
(1007, 'Machine Learning',5000,3000000),
(1008, 'Git and Github',500,1000000),
(1009, 'C++',1000,30000),
(1010, 'Data Science',5000, 378678)
Output:
coursesActive TableExplanation: This query inserts all the rows with the given values, if the column count doesn't match then it will throw an error or if the column that was getting inserted has the wrong datatype than expected then it will throw an error.
Inserting Multiple Rows with Some Columns Only:
Let's insert some data into some columns of the table while the remaining columns left in the table place NULL values in it except Primary Key. Because the Primary Key always holds some values in it otherwise it will throw an error.
Query:
INSERT INTO CoursesActive (courseId,courseName)
VALUES (1011, 'DevOps'),
(1012, 'C#'),
(1013, '.NET');
SELECT * FROM CoursesActive
Output :
ResultExplanation: This query inserts only the mentiond columns and those mentioned columns are mandatory, other than the mentioned columns remaining will be NULL.
Inserting Multiple Rows Using SELECT and UNION:
With the help of UNION we can also insert data into the table. Let's check with an example.
Query:
INSERT INTO CoursesActive
SELECT 1014, 'DevOps Advanced', 3000, 20000
UNION
SELECT 1015, 'Data Science Advanced', 5000, 2000000
SELECT * FROM CoursesActive
Output :
ResultExplanation: Using the UNION, we are combine the multiple select statements with the rows information and then later the INSERT command inserts the rows into the table. In the result, we can see that 2 rows were added at the end.
Example: Let's take an example of adding the three rows with only courseId, courseName, studentsEnrolled fields into the CoursesActive Table.
Query:
INSERT INTO CoursesActive (courseId,courseName, studentsEnrolled)
VALUES (1016, 'DevOps', 100000),
(1017, 'C#' ,200000),
(1018, '.NET',230887);
Output:

Explanation: In the explanation we have explicitly mentioned the column names that we want to insert and the three rows are inserted with the columns courseId, courseName, studentsEnrolled other than them the remaining is NULL.
Conclusion
Using the INSERT statement we can insert multiple rows into the table without adding multiple INSERT statements for each row. It is one of the most used and necessary DML commands for starting with the SQL Server. It helps us in adding multiple columns or only the required columns of the table easily.
Similar Reads
SQL Query to Insert Multiple Rows
In SQL, the INSERT statement is used to add new records to a database table. When you need to insert multiple rows in a single query, the INSERT statement becomes efficient. In this article, We will learn different methods such as using basic INSERT statements, utilizing INSERT INTO ... SELECT for b
4 min read
How to Insert Multiple Rows in SQLite?
In the area of database management, efficiency is key. When working with SQLite, a lightweight database engine, inserting multiple rows efficiently can significantly boost performance and speed our workflow. In this article, We will understand how to insert multiple rows in SQLite through various me
3 min read
MySQL Insert Multiple Rows
MySQL is an open-source Relational Database Management System that stores data in rows and columns. MySQL is designed to be platform-independent, which means it can run on various operating systems, including Windows, Linux, macOS, and more. MySQL is scalable and can handle databases of varying size
5 min read
PL/SQL INSERT INTO SELECT
In PL/SQL, the INSERT INTO SELECT statement is used to insert data into a table by selecting data from one or more tables. This is a powerful feature for populating tables with data from existing tables or views, making it useful for data migration, reporting, and backup processes. In this guide, we
5 min read
How to Concatenate Text From Multiple Rows in SQL Server
When we fetch data from a table, there may be requirements to concatenate the text value of a table column in multiple rows into a single row. There are many ways we can concatenate multiple rows into single row SQL Server. We can use different ways based on need and convenience. In this article, we
6 min read
How to Insert Multiple Rows at Once in PL/SQL?
As the volume and complexity of data continue to grow in modern systems, efficient data management techniques become important. One fundamental operation in database management is the insertion of multiple rows at once. In this article, we understand the techniques and methods available in PL/SQL fo
5 min read
PostgreSQL - Insert Multiple Values in Various Rows
PostgreSQL, one of the most popular relational database management systems (RDBMS), is widely used for storing structured data in a tabular format, much like MySQL. In relational databases, data is stored in tables where each row represents a record and each column represents an attribute. One of th
3 min read
Insert Statement in MS SQL Server
The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table. In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the hel
4 min read
PostgreSQL - INSERT
PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
5 min read
Multiple Joins in SQL
Structured Query Language or SQL is a standard database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. In this article, we will be using the Microsoft SQL Server. Multiple Joins :Here we are going to implement the concept of multipl
3 min read