Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 44

Insert Multiple Rows in MySQL

In this lesson: Insert many rows in MySQL with a single INSERT statement, plus bulk-insert tips and output. Copy the query now. In Hindi & English.

Adding many rows

You often need to add many rows at once — a list of students, products or records. Running one INSERT per row works but is slow. MySQL lets you insert many rows in a single INSERT, which is faster and cleaner.

💡 One INSERT with many value sets is much faster than many separate INSERTs.

One row at a time

INSERT INTO students (roll, name) VALUES (1, 'Aman');
INSERT INTO students (roll, name) VALUES (2, 'Diya');
INSERT INTO students (roll, name) VALUES (3, 'Kabir');

This works, but each statement is processed separately. For a few rows it is fine; for many, it is slow and repetitive.

Many rows in one INSERT

INSERT INTO students (roll, name) VALUES
    (1, 'Aman'),
    (2, 'Diya'),
    (3, 'Kabir');
Query OK, 3 rows affected (0.01 sec)

List each row's values in parentheses, separated by commas. All three rows are added in one statement — shorter to write and much faster to run.

Why it is faster

A single multi-row INSERT is processed as one operation, with far less overhead than sending and running many separate statements. For loading hundreds or thousands of rows, this makes a huge speed difference — one of the easiest performance wins in SQL.

Insert from a SELECT

INSERT INTO archive (id, name)
SELECT id, name FROM users WHERE active = 0;

INSERT INTO ... SELECT copies rows from one table into another. Here inactive users are copied into an archive table in a single statement — no manual typing of values.

Handling duplicates

-- skip rows that clash with a unique key
INSERT IGNORE INTO students (roll, name) VALUES (1, 'Aman');

-- or update the existing row instead
INSERT INTO students (roll, name) VALUES (1, 'Aman Kumar')
    ON DUPLICATE KEY UPDATE name = 'Aman Kumar';

INSERT IGNORE skips rows that would cause a duplicate-key error, and ON DUPLICATE KEY UPDATE updates the existing row. Both let a bulk insert continue smoothly past duplicates.

Common mistakes

  • Mismatched value counts. Each row's values must match the listed columns.
  • Forgetting commas between rows. Separate each parenthesised set with a comma.
  • Ignoring duplicate-key errors. Use INSERT IGNORE or ON DUPLICATE KEY UPDATE.
  • One giant statement for millions of rows. Insert in batches to respect packet size.

Summary

  • Insert many rows in one INSERT by listing value sets with commas.
  • A single multi-row INSERT is much faster than many separate ones.
  • INSERT INTO ... SELECT copies rows from another table.
  • INSERT IGNORE and ON DUPLICATE KEY UPDATE handle duplicates.
  • For huge loads, insert in batches.

Next, update many rows in updating multiple rows, or auto-number them with AUTO_INCREMENT.

Frequently Asked Questions

How do I insert multiple rows in MySQL?
List several value sets separated by commas in one INSERT: INSERT INTO t (a, b) VALUES (1,'x'), (2,'y'), (3,'z');. This adds all the rows in a single statement, which is much faster than one INSERT per row.
Why is inserting many rows at once faster?
A single multi-row INSERT is faster because MySQL processes it as one operation, with less overhead than running many separate INSERT statements. For large loads, this can be dramatically quicker.
How do I insert rows from another table?
Use INSERT INTO ... SELECT: INSERT INTO archive (id, name) SELECT id, name FROM users WHERE active = 0;. This copies matching rows from one table into another in a single statement.
How do I avoid errors from duplicate rows during insert?
Use INSERT IGNORE to skip rows that would cause a duplicate-key error, or INSERT ... ON DUPLICATE KEY UPDATE to update the existing row instead. This lets a bulk insert continue past duplicates.
Is there a limit to how many rows I can insert at once?
There is no fixed row limit, but very large statements are limited by the max_allowed_packet setting (the maximum query size). For huge loads, insert in batches of a few thousand rows to stay within limits and keep it efficient.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicSELECT Command Your progress: 44 / 151 (29%) · Lesson 45 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free