Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL INSERT Multiple Rows: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert multiple rows into a SQL table in one statement, use a single INSERT INTO with a comma-separated list of value tuples: INSERT INTO table (col1, col2) VALUES (1, 'a'), (2, 'b'), (3, 'c');. This is faster and cleaner than running several separate INSERT statements, and it is supported by MySQL, PostgreSQL, SQLite, SQL Server (from 2008 onward), and Oracle (using a different syntax). You need a table that already exists, matching column names, and values that fit the column data types.

What You'll Need

  • A database connection with permission to insert into the target table.
  • The table name and the exact column names you plan to fill.
  • Values in the correct order and data type (numbers without quotes, text in single quotes, dates in the format your database expects).
  • A SQL client or command-line tool (for example, MySQL Workbench, pgAdmin, SQLiteStudio, or Azure Data Studio).

Step-by-Step

  1. Check the table structure. Run a command such as DESCRIBE table_name; (MySQL) or \d table_name (PostgreSQL) to confirm column names, order, and which columns allow NULL or have defaults. This prevents mismatched values.
  2. Write the INSERT statement with column names. Always list the columns explicitly: INSERT INTO customers (first_name, last_name, email) VALUES (...); This protects you if the table gains columns later and makes the statement self-documenting.
  3. Add multiple value tuples separated by commas. Each tuple is a set of parentheses with one value per listed column: VALUES ('Ana', 'Lopez', 'ana@example.com'), ('Ben', 'Ng', 'ben@example.com'), ('Cara', 'Singh', 'cara@example.com'); End the whole statement with a semicolon.
  4. Match data types and quote text. Numbers and booleans go unquoted; strings and dates go in single quotes. Use two single quotes to escape an apostrophe inside a string (for example, 'O''Brien'). Never wrap numbers in quotes unless the column is a text type.
  5. Run the statement and check the row count. Most clients report something like “3 rows affected.” If the count is lower than expected, a constraint or trigger may have rejected or merged rows.
  6. Verify with a SELECT. Query the inserted rows (for example, SELECT * FROM customers WHERE email IN ('ana@example.com','ben@example.com','cara@example.com');) to confirm the values landed correctly.
  7. For very large batches, wrap in a transaction. In PostgreSQL or SQLite, BEGIN; before the INSERT and COMMIT; after lets you roll back cleanly if one row fails. In MySQL with InnoDB, use START TRANSACTION; and COMMIT;. This keeps partial inserts from being left behind.
  8. Use the database-specific alternative if needed. Oracle does not accept multiple tuples in a single VALUES clause; use INSERT ALL INTO t (c1,c2) VALUES (1,'a') INTO t (c1,c2) VALUES (2,'b') SELECT 1 FROM dual; or INSERT INTO t (c1,c2) SELECT 1,'a' FROM dual UNION ALL SELECT 2,'b' FROM dual;. SQL Server supports up to 1,000 tuples per INSERT statement; split larger batches.

Common Mistakes

  • Forgetting commas between tuples. Each tuple must be separated by a comma; a missing comma causes a syntax error near the next parenthesis.
  • Column count mismatch. Every tuple must supply exactly as many values as the columns listed. Mixing three values in one tuple and two in another fails.
  • Omitting the column list. Relying on table column order breaks when the schema changes and is hard to read.
  • Quoting numbers or not quoting text. '123' inserted into an integer column may work through implicit conversion in some databases but fails in others; be explicit.
  • Ignoring constraints. Duplicate primary keys, NOT NULL violations, or foreign key failures abort the whole statement (or the row, depending on the database and settings).
  • Exceeding statement limits. SQL Server caps a single INSERT at 1,000 rows; other databases have packet-size limits. Split large batches.

Tips

  • Use a single multi-row INSERT instead of many single-row INSERTs when loading moderate batches; it reduces round trips and is usually faster.
  • For very large imports, use the bulk loader (for example, COPY in PostgreSQL, LOAD DATA in MySQL, or bcp in SQL Server) rather than a giant INSERT.
  • Keep the column list in the same order as your values; a comment above the statement helps future readers.
  • Test on a development copy first, especially when inserting into tables with triggers or cascading foreign keys.

Troubleshooting

If the statement fails, read the error message carefully: it usually names the column, constraint, or line. A syntax error near a comma often means a missing or extra parenthesis. A “column count doesn't match” error means the number of values in one tuple differs from the column list. A duplicate key error means one of your rows already exists; use INSERT ... ON CONFLICT DO NOTHING (PostgreSQL/SQLite) or INSERT IGNORE (MySQL) only if skipping duplicates is intended. If the row count is zero, check that you are connected to the right database and schema, and that autocommit is on (or that you committed the transaction). If a foreign key fails, confirm the referenced parent row exists before inserting the child. If the client truncates a long statement, split it into smaller batches.

Frequently Asked Questions

Can I insert multiple rows without listing column names?

Yes, but you must supply values for every column in the table's defined order. This is fragile and not recommended.

How many rows can one INSERT statement handle?

It varies by database and client. SQL Server allows up to 1,000 tuples per statement; MySQL and PostgreSQL are limited mainly by packet size. Split large batches.

Does a multi-row INSERT run as one transaction?

In most databases a single statement is atomic: either all rows are inserted or none are. Wrap multiple statements in an explicit transaction if you need that guarantee across statements.

How do I insert multiple rows and get the generated IDs?

Use RETURNING in PostgreSQL or SQLite, or OUTPUT INSERTED.id in SQL Server. MySQL does not return all generated IDs from a multi-row INSERT; query them afterward or insert one row at a time if you need each ID.

Why did only some of my rows insert?

If the database is not in strict mode or uses per-row error handling, invalid rows may be skipped. Check the warning count and the error log, and fix the offending values.

Can I use a subquery to supply multiple rows?

Yes: INSERT INTO t (c1,c2) SELECT c1,c2 FROM source WHERE ...; inserts as many rows as the SELECT returns.