← Back to MySQL Course | Chapter 5: Inserting & Selecting Data | Lesson 2 of 6

INSERT Multiple Rows

Multiple Rows in One Query

A single INSERT INTO statement can add many rows at once by listing several comma-separated value sets in parentheses, which is both more concise and considerably faster than one INSERT per row.

Example: Multiple Rows in One Query

sql
INSERT INTO users (name) VALUES ('Amit'), ('Priya'), ('Rahul');

Efficiency Benefits

Batching inserts together significantly reduces the number of round trips between your application and the database server, cutting network overhead and letting MySQL optimize the whole batch as one operation.

Example: Efficiency Benefits

sql
-- One round trip inserts all three rows instead of three separate statements
INSERT INTO users (name) VALUES ('Amit'), ('Priya'), ('Rahul');

Mixing Columns and Multiple Rows

Every value set in a multi-row insert must supply values in the same order and for the same columns as the others — mismatched column counts across rows will cause the entire statement to fail.

Example: Mixing Columns and Multiple Rows

sql
INSERT INTO users (id, name) VALUES (1, 'Amit'), (2, 'Priya');

Inserting Nulls in Multiple Rows

You can still use the NULL keyword inside any individual row of a multi-row insert, letting some rows have missing data for a column while others in the same batch don't.

Example: Inserting Nulls in Multiple Rows

sql
INSERT INTO users (id, name) VALUES (1, 'Amit'), (2, NULL);

Handling Auto-Increment

When the primary key is AUTO_INCREMENT, you simply omit it from every row in a multi-row insert, and MySQL assigns the next sequential ID to each one automatically as it processes the batch.

Example: Handling Auto-Increment

sql
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
INSERT INTO users (name) VALUES ('Amit'), ('Priya');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.