How to Insert Multiple Rows in a Single SQL Statement

Posted by Kyle Hankinson


Short answer

List several rows after VALUES, separated by commas:

INSERT INTO products (name, price, category)
VALUES
    ('Widget',    9.99,  'hardware'),
    ('Gadget',    24.99, 'electronics'),
    ('Gizmo',     14.99, 'hardware'),
    ('Doohickey', 4.99,  'accessories');

This works in MySQL, MariaDB, PostgreSQL, SQL Server (2008 and later), SQLite (3.7.11 and later) and Oracle Database 23ai. It is one statement and one round trip to the server, so it is much faster than four separate INSERTs.

Oracle versions before 23ai need INSERT ALL instead (below).

Oracle before 23ai: INSERT ALL

INSERT ALL
    INTO products (name, price, category) VALUES ('Widget', 9.99, 'hardware')
    INTO products (name, price, category) VALUES ('Gadget', 24.99, 'electronics')
    INTO products (name, price, category) VALUES ('Gizmo', 14.99, 'hardware')
SELECT 1 FROM DUAL;

The closing SELECT 1 FROM DUAL is required: INSERT ALL runs its INTO clauses once for each row the query returns, and DUAL returns exactly one row.

An alternative that works on any Oracle version is INSERT ... SELECT with UNION ALL:

INSERT INTO products (name, price, category)
SELECT 'Widget', 9.99, 'hardware' FROM DUAL UNION ALL
SELECT 'Gadget', 24.99, 'electronics' FROM DUAL;

Copy rows from another table: INSERT ... SELECT

INSERT INTO products_archive (name, price, category)
SELECT name, price, category
FROM products
WHERE discontinued = 1;

This works the same way in every database, and has no row limit.

How many rows can one statement hold?

Database Limit
SQL Server 1,000 rows per VALUES list (error 10738 above that)
MySQL / MariaDB The whole statement must fit in max_allowed_packet (64 MB by default in MySQL 8)
PostgreSQL No row limit for literal values; with bound parameters, 65,535 parameters per statement
SQLite No row limit for literal values; with bound parameters, 32,766 per statement (999 before SQLite 3.32)

To insert 1,000 rows or more, send several statements of a few hundred to 1,000 rows each, inside one transaction:

BEGIN;
INSERT INTO products (name, price) VALUES ('Item 1', 1.00), ('Item 2', 2.00) /* ... up to 1,000 rows */;
INSERT INTO products (name, price) VALUES ('Item 1001', 1.00) /* ... next batch */;
COMMIT;

(MySQL, PostgreSQL and SQLite accept BEGIN; SQL Server uses BEGIN TRANSACTION.)

Wrapping the batches in a transaction matters as much as the batching: without it, each statement is committed on its own.

Get the generated IDs back

-- PostgreSQL, SQLite 3.35+, MariaDB 10.5+
INSERT INTO products (name, price)
VALUES ('Widget', 9.99), ('Gadget', 24.99)
RETURNING id, name;

-- SQL Server
INSERT INTO products (name, price)
OUTPUT inserted.id, inserted.name
VALUES ('Widget', 9.99), ('Gadget', 24.99);

MySQL has no RETURNING. After a multi-row insert, LAST_INSERT_ID() returns the id of the first inserted row, not the last.

Loading thousands or millions of rows

Multi-row INSERT is fine into the tens of thousands. Beyond that, use the database's bulk loader, which reads a file directly:

-- MySQL (needs local_infile enabled on client and server for LOCAL)
LOAD DATA LOCAL INFILE '/path/to/products.csv' INTO TABLE products
FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES;

-- PostgreSQL (psql's \copy reads the file from your machine)
\copy products FROM '/path/to/products.csv' CSV HEADER

-- SQL Server 2017+ (the path is on the server)
BULK INSERT products FROM 'C:\data\products.csv' WITH (FORMAT = 'CSV', FIRSTROW = 2);

For upserting many rows at once (insert new ones, update existing ones), see insert into a MySQL table or update if exists.


About the author - Kyle Hankinson is the founder and sole developer of SQLPro for MySQL and the Hankinsoft Development suite of database tools. He has been building native macOS and iOS applications since 2010, and now ships SQLPro on Windows and Android as well.

Try SQLPro for MySQL - A native MySQL and MariaDB client for macOS, iOS, and Android. No Java required.

Download Free Trial View Pricing Compare