Tutorials  /  Databases

How to INSERT Multiple Records in SQL: Methods & Examples

Ccentron Redaktion · December 2024 ·3 min read ·Databases, Tutorial

Need of SQL Insert INTO Multiple Rows Query

SQL INSERT query inserts data into the columns of a particular table.

The normal SQL INSERT query inputs the data values in a single row. In case when we want to insert data in multiple rows at once, this query fails.

Thus, in order to save the execution time, we need to use the SQL INSERT query in such a manner that it injects data into multiple rows at once.

Having understood the need of SQL Insert query for multiple rows, let us get started with the implementation of the same.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Traditional SQL INSERT Query to Insert Multiple Records

Traditional SQL INSERT query injects input data into multiple rows. In this technique, we need to the insert query as many times we want to input data into the rows of the table.

The basic drawback of this query is the overhead of execution of every insert query for multiple rows injection.

Example:

SQL
create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 50, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Pune");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
select * from Info;

Output:

1 100 Pune

2 50 Satara

3 65 Pune

4 97 Mumbai

5 12 USA

INSERT-SELECT-UNION Query to Insert Multiple Records

In the above section, we got to know that INSERT INTO query injects multiple records. But, if we observe the output of it, we get to know that the clause ‘INSERT INTO’ is repeated many times.

Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table.

The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

SQL
create table Info(id integer, Cost integer);
INSERT INTO Info (id, Cost)  
SELECT 1, '123'  
UNION ALL   
SELECT 2, '234'  
UNION ALL  
SELECT 3, '456';  

select * from Info;

Output:

1 123

2 234

3 456

Row Construction to Insert Multiple Records

The SQL INSERT query is used in a manner wherein we make use of a single INSERT query to insert multiple records within a single point of execution.

Syntax:

SQL
INSERT INTO Table (columns)  
VALUES (val1, val2, valN); 

Example:

SQL
create table Info(id integer, Cost integer,city nvarchar(200));
INSERT INTO Info (id,Cost,city)  
VALUES (1,200, 'Pune'), (2, 150,'USA'), (3,345, 'France');  

select * from Info;

Output:

1 200 Pune

2 150 USA

3 345 France

Conclusion

By this we have come to the end of this topic. Herein we have covered three different techniques to INSERT data values across multiple records of a table.

Claim €200 credit

Test your setup on ccloud³

Sign up for ccloud³ and receive €200 starting credit for your project.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Get started for free

Sign up and receive €200 credit at centron within your first 60 days.

This promotional offer applies to new accounts only. Available exclusively to businesses.