
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL Query to Insert Multiple Rows
When we work with database tables, we need to insert multiple rows into database tables. We can improve the efficiency of SQL queries if we use an efficient method to insert multiple rows into database tables. We can reduce the number of queries sent to the server.
We will discuss different methods and their advantages for inserting multiple rows into database tables. You can select your best method depending on your data and your choice.
We should follow practices when we insert data into database tables -
- We should prepare statements for dynamic data to avoid SQL injection.
- We should insert batch by batch to insert large data into database tables.
- We should define constraints and indexes to keep data integrity.
Now, we will discuss different methods to insert rows into database tables.
Create Database Table
First, we need to create a sample table. We will create Author table to discuss examples in this article. You can create this table using below SQL query -
CREATE TABLE Author( [ID] INT identity(1,1), [FirstName] VARCHAR(100), [LastName] VARCHAR(100), [Country] VARCHAR(100) );
It will create Author table with ID, FirstName, LastName, and Country attributes. ID will be the primary key because of the unique number for each row. It will be generated automatically, so we do not insert this ID when we insert data into the Author table.
Now, we will insert data into Author using different methods as discussed below.
Using Multiple INSERT Statements
This is a simple method to insert multiple rows into Author table. We use INSERT statement for every row to insert data. You can use the below query to insert multiple rows into Author table given below -
INSERT INTO Author ([FirstName], [LastName], [Country]) VALUES ('Mithlesh', 'Upadhyay', 'India'); INSERT INTO Author ([FirstName], [LastName], [Country]) VALUES ('Rudra', 'Upadhyay', 'India'); INSERT INTO Author ([FirstName], [LastName], [Country]) VALUES ('Chandan', 'Gautam', 'India');
It will insert above three rows into Author table -
You can check these rows using below SQL query -
SELECT * FROM Author
The output will be -
ID | FirstName | LastName | Country |
1 | Mithlesh | Upadhyay | India |
2 | Rudra | Upadhyay | India |
3 | Chandan | Gautam | India |
This method to insert multiple rows into a database table is easy but it is not an efficient method because if we have large data to insert into the Author table, then we need to write INSERT INTO Author ([FirstName], [LastName], [Country]) VALUES () for every row. Hence, we do not prefer this method to insert large data into the database table.
Single INSERT Statement with Multiple Rows
We can insert multiple rows using single INSERT statement and then put each value using commas. This method is efficient than Method 1 we discussed as above. You can use the below query to insert multiple rows into Author table -
INSERT INTO Author ([FirstName], [LastName], [Country]) VALUES ('Kiran', 'Tiwari', 'Nepal'), ('Mithlesh', 'Upadhyay', 'US'), ('Chandan', 'Gautam', 'US');
It will insert the above three rows into Author table. You can check these rows using below SQL query -
SELECT * FROM Author
The output Author table will be -
ID | FirstName | LastName | Country |
1 | Mithlesh | Upadhyay | India |
2 | Rudra | Upadhyay | India |
3 | Chandan | Gautam | India |
4 | Kiran | Tiwari | Nepal |
5 | Mithlesh | Upadhyay | US |
6 | Chandan | Gautam | US |
We reduced the number of queries sent to the database. This method is more efficient than the previous method. Now, let's clean the Author table before we start the next method in the discussion. You can clean Author table using below SQL query -
TRUNCATE TABLE Author
It will remove all the data rows data inside Author table except the schema of Author table.
Using INSERT INTO SELECT Statement
We can insert rows of another table into the Author table. We use the INSERT INTO SELECT statement for this purpose. We use this method when data is large to insert into a database table. For example, we will create TempAuthor table to copy rows of this table into our Author table. You can create TempAuthor table using below SQL query -
CREATE TABLE TempAuthor ( [FirstName] VARCHAR(100), [LastName] VARCHAR(100), [Country] VARCHAR(100) ); INSERT INTO TempAuthor ([FirstName], [LastName], [Country]) VALUES ('Mithlesh', 'Upadhyay', 'India'), ('Rudra', 'Upadhyay', 'India'), ('Chandan', 'Gautam', 'India');
It will create TempAuthor with above three rows. You can check TempAuthor table using below SQL query -
SELECT * FROM TempAuthor
The output is -
FirstName | LastName | Country |
Mithlesh | Upadhyay | India |
Rudra | Upadhyay | India |
Chandan | Gautam | India |
Now, we can insert these rows from TempAuthor table Author table. You can use below SQL query below to do this -
INSERT INTO Author ([FirstName], [LastName], [Country]) SELECT [FirstName], [LastName], [Country] FROM TempAuthor;
It will add all these rows from TempAuthor to Author. We use FirstName, LastName, and Country attributes. You can check Author table using below SQL query -
SELECT * FROM Author
The output will be -
ID | FirstName | LastName | Country |
1 | Mithlesh | Upadhyay | India |
2 | Rudra | Upadhyay | India |
3 | Chandan | Gautam | India |
Now, we will see the next method to insert multiple rows into the database table.
Using Stored Procedure
We can add rows dynamically. We use stored procedures to insert dynamic data into database tables. For example, we create the InsertAuthors procedure database table. You can use below SQL query below to insert dynamic data -
CREATE PROCEDURE InsertAuthors @FirstName VARCHAR(100), @LastName VARCHAR(100), @Country VARCHAR(100) AS BEGIN INSERT INTO Author ([FirstName], [LastName], [Country]) VALUES (@FirstName, @LastName, @Country); END;
You can execute this stored procedure for multiple rows.
Conclusion
We have discussed different methods to insert multiple rows into database tables. We used examples to explain the concept in each method. You can choose any of these methods according to your data and choice.