Inserting Records (INSERT INTO, Single vs Multi-Row Inserts)
Insert one or many rows efficiently and return generated keys when needed.
In plain words
INSERT adds new rows to a table. You can add one, many at once, or copy them from another query.
Think of it like…
Posting letters: walking to the post box once per letter takes all day, while dropping a whole bundle in at once takes a minute. Multi-row inserts are the bundle.
Where you’ve seen it
Creating an order and getting its id: INSERT INTO orders (customer_id, amount) VALUES ($1, $2) RETURNING id, created_at;. The application then inserts the items using that id, inside the same transaction.
Words you’ll hear
- Column list
- The explicit list of columns being filled, in the same order as the values.
- Multi-row VALUES
- VALUES (…), (…), (…) in one statement.
- INSERT … SELECT
- Inserts the result of a query.
- RETURNING
- Returns columns of the inserted rows (PostgreSQL); MySQL uses LAST_INSERT_ID().
Visual diagram
Theory & example queries
Writing inserts well
Always list the columns: INSERT INTO users (email, name) VALUES ($1, $2). Without a list, values are matched to columns by position, so adding or reordering columns later silently puts data in the wrong place or breaks the query. Columns you leave out take their DEFAULT, and you can write DEFAULT explicitly in VALUES too.
INSERT … SELECT copies data in one statement without moving it through the application, for example archiving rows or filling a summary table. With RETURNING, PostgreSQL returns any columns of the new rows, including identity values, defaults and trigger-set values, which avoids a second query.
| name | |
|---|---|
| asha@example.com | Asha |
INSERT INTO users (email, name)VALUES ('asha@example.com', 'Asha')RETURNING id, created_at;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative returned row1 sample row
| id | created_at |
|---|---|
| 1042 | 2026-11-21T13:45:12+00:00 |
Note: The identity and timestamp are illustrative; PostgreSQL RETURNING reports values generated or set by defaults and triggers.
Key takeaway: Name the columns, and use RETURNING instead of a follow-up SELECT.
Inserting many rows fast
Each separate INSERT costs a network round trip and, in autocommit mode, a transaction commit that waits for the log to reach disk. Inserting 10,000 rows one at a time can take tens of seconds; the same rows in multi-row statements of 1,000, inside one transaction, take well under a second.
For very large loads, the bulk loaders are faster still: COPY in PostgreSQL and LOAD DATA INFILE in MySQL. Keep batches to a reasonable size (very large statements use a lot of memory and hold locks longer), and consider dropping or deferring non-essential indexes during huge one-off loads.
| id | order_id | product_id | qty | price |
|---|---|---|---|---|
| 1 | 101 | 42 | 2 | 49.5 |
| 2 | 101 | 84 | 1 | 150 |
| 3 | 102 | 42 | 1 | 49.5 |
INSERT INTO order_items (order_id, product_id, qty)VALUES (501, 11, 2), (501, 42, 1), (501, 77, 3);PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative affected-row status
Note: The statement affects three rows. PostgreSQL shows INSERT 0 3; other clients or dialects report an equivalent count of three inserted rows.
Key takeaway: Batch rows into fewer statements and transactions; use COPY for bulk loads.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
List the columns
Name each column you're filling, in the same order as the values.
INSERT INTO orders (customer_id, amount)When to use it & trade-offs
What it is
INSERT INTO table (columns) VALUES (…) adds rows. One statement can insert many rows with several value lists, INSERT … SELECT copies rows from a query, and RETURNING (PostgreSQL) gives back generated values such as the new id. Columns left out get their DEFAULT, or NULL if there isn't one.
Why it matters: Inserting one row per round trip is one of the most common causes of slow imports and slow APIs. Listing columns explicitly, batching rows and getting generated keys back correctly are everyday backend skills that interviewers expect.
When to reach for it
- Creating records from API requests
- Importing data in batches
- Copying or archiving rows with INSERT … SELECT
When to avoid or be careful
- INSERT without a column list
- Looping single-row inserts in autocommit mode for large imports
- A follow-up SELECT MAX(id) to find the new id (racy under concurrency)
Single-row INSERT vs multi-row INSERT vs COPY
Strengths: Simple and clear.
Watch out: Very slow when repeated thousands of times.
Strengths: Far fewer round trips and commits.
Watch out: Very large batches use memory and hold locks longer.
Strengths: Fastest way to load data.
Watch out: Different syntax; less row-by-row control over errors.
Interview signals & numbers to know
When this topic comes up
- “The import takes hours”
- “How do I get the id of the inserted row?”
- Bulk loading
- INSERT … SELECT
- Batch processing
Benchmarks & rules of thumb
- 1
- round trip for a multi-row insert
- ~1,000
- rows per batch is a sensible starting point
- RETURNING
- PostgreSQL's way to get generated keys
Common mistakes & follow-ups
Common interview mistakes
- Writing INSERT INTO users VALUES (…) and breaking it when a column is added.
- Finding the new row with SELECT MAX(id), which can return another session's row.
- Inserting 100,000 rows one statement at a time in autocommit mode.
Likely interviewer follow-ups
- How would you speed up an import of a million rows?
- How do you get the generated id after an insert in PostgreSQL and in MySQL?
- Why should you always list the columns in an INSERT?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Inserting Records (INSERT INTO, Single vs Multi-Row Inserts) with hands-on SQL practice and interview Q&A.