Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 11/ 160
01. SQL Foundations & Relational Concepts

Inserting Records (INSERT INTO, Single vs Multi-Row Inserts)

Insert one or many rows efficiently and return generated keys when needed.

BeginnerINSERT INTO · Multi-Row Insert3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Insert and get the generated values backQuery example
1Input dataStart from the source rows or state that the statement sees.
input values
1 row
input values sample rows
emailname
asha@example.comAsha
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1INSERT INTO2  users (email, name)3VALUES4  ('asha@example.com', 'Asha')5RETURNING6  id,7  created_at;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/7
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative returned row1 sample row
Representative returned row
idcreated_at
10422026-11-21T13:45:12+00:00

Note: The identity and timestamp are illustrative; PostgreSQL RETURNING reports values generated or set by defaults and triggers.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

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.

A multi-row insertQuery example
1Input dataStart from the source rows or state that the statement sees.
order_items
3 rows
order_items sample rows
idorder_idproduct_idqtyprice
110142249.5
2101841150
310242149.5
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1INSERT INTO2  order_items (order_id, product_id, qty)3VALUES4  (501, 11, 2),5  (501, 42, 1),6  (501, 77, 3);
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/6
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative affected-row status
INSERT 0 3

Note: The statement affects three rows. PostgreSQL shows INSERT 0 3; other clients or dialects report an equivalent count of three inserted rows.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Batch rows into fewer statements and transactions; use COPY for bulk loads.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Statement

List the columns

Name each column you're filling, in the same order as the values.

SQL at this step
1INSERT INTO2  orders (customer_id, amount)
What to watch at this step
Never
rely on column position
Tip: use ← → to move between steps

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

Single-row INSERT
Best for: One record per user action

Strengths: Simple and clear.

Watch out: Very slow when repeated thousands of times.

Multi-row INSERT / INSERT … SELECT
Best for: Batches from the application, or copying between tables

Strengths: Far fewer round trips and commits.

Watch out: Very large batches use memory and hold locks longer.

COPY / LOAD DATA
Best for: Loading files with millions of rows

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?
Interview tip: Mention three habits: list the columns, use RETURNING for generated keys, and batch inserts for imports. It's a short answer that shows practical experience.

Knowledge check

Loading quiz

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.

Open SQL Practice →SQL interview Q&A
Previous topic
Data Constraints (NOT NULL, UNIQUE, CHECK, DEFAULT)
Next topic
Updating Records Safely (UPDATE with WHERE)