Learn SQL
-- tables.insert_data

INSERT — Adding Rows in SQL

Put new records into a table

--concept

INSERT adds new rows to a table. You name the target table, optionally the columns you are supplying, and VALUES for one or many rows. Columns you skip receive their DEFAULT (or NULL). Modern SQL inserts multiple rows in a single statement, which is far faster than looping.

--how it works
1

Name table and columns

INSERT INTO employees (id, name, department, salary, age) — always list columns explicitly; it survives schema changes and reads clearly.

2

Supply matching VALUES

One tuple per row, same order as your column list. Mismatched count or a type violation aborts the whole statement.

3

Insert many rows at once

Separate tuples with commas — one statement, many rows. Massively faster than looping single INSERTs.

4

Let defaults fill gaps

Any column you omit receives its DEFAULT, or NULL if none was declared — that is how bonus stays empty for most employees.

5

Respect the constraints

Duplicate primary keys or NOT NULL violations fail the entire multi-row insert — the table never ends up half-changed.

--visualization
idnamedepartmentsalary1AliceEngineering850002BobSales520003CynthiaEngineering910004DavidMarketing470005ElenaSales580006FaridEngineering73000green = newly inserted rows
idnamedepartmentsalaryage
Stage 1 / 4
Current table: 6 employees. We will add Grace and Hassan in ONE statement.
--example
query.sql
1INSERT INTO employees (id, name, department, salary, age)
2VALUES
3 (7, 'Grace', 'Engineering', 81000, 31),
4 (8, 'Hassan', 'Sales', 49500, 24);
Result
idnamedepartmentsalaryage
7GraceEngineering8100031
8HassanSales4950024

Two complete rows arrive in one statement. Omitting a column (say age) would store NULL there unless a DEFAULT was declared.

--variants

Single row

The plainest insert — full column list, one tuple.

query.sql
1INSERT INTO employees (id, name, department, salary, age)
2VALUES (7, 'Grace', 'Engineering', 81000, 31);
Result
idnamedepartmentsalaryage
7GraceEngineering8100031
1The column list and the tuple agree one-for-one.
2The engine checks the primary key, then the types, then writes.
3It reports "1 row inserted" — always read that number.

Multi-row insert

Many tuples, one statement — one round trip, one transaction.

query.sql
1INSERT INTO employees (id, name, department, salary, age)
2VALUES
3 (8, 'Hassan', 'Sales', 49500, 24),
4 (9, 'Iris', 'Marketing', 61000, 27);
Result
idnamedepartmentsalaryage
8HassanSales4950024
9IrisMarketing6100027
1Both tuples are checked before anything is written.
2One id colliding would abort BOTH rows — insert is atomic per statement.
3Batching cuts network round trips and per-statement overhead dramatically.

Partial insert

Omitted columns fall back to DEFAULT or NULL.

query.sql
1INSERT INTO employees (id, name, department, salary)
2VALUES (10, 'Jun', 'Sales', 47000);
Result
idnamedepartmentsalaryage
10JunSales47000NULL
1age is not in the column list, so it stores NULL — meaning "unknown", not zero.
2The bonus column (not shown) is likewise NULL.
3Downstream, remember IS NULL tests, not age = NULL.

Constraint failure

A duplicate key fails the whole statement.

query.sql
1INSERT INTO employees (id, name, department, salary, age)
2VALUES (7, 'Impostor', 'Sales', 100, 99);
3-- ERROR: duplicate key id = 7
1id 7 already exists (Grace), so the engine rejects the row.
2Nothing is written — not even the "valid" parts of the statement.
3Fix by using a new id, or use INSERT OR IGNORE / ON CONFLICT where supported.
--key facts
Multi-rowone INSERT with many VALUES tuples beats N separate statements
Safetyalways list columns explicitly — table layouts change
Feedbackthe engine reports how many rows were inserted
--use cases
User sign-ups and order creation in any application
Bulk-loading a CSV export into a staging table
Seeding test fixtures for development and QA
Logging pipelines that batch many events per statement
--under the hood
Multi-row INSERTs cut per-statement overhead; for thousands of rows engines offer COPY (Postgres) or batched prepared statements.
Listing columns explicitly is free insurance: someone adds a column next year and your INSERT keeps working.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.