INSERT: one row, multiple rows, and rows from SELECT
Chapter 8 — “Write Access”
QUERY leads you down a corridor off-limits to cadets with “read-only” clearance. Beyond the bulkhead, the sandbox compartment hums: an exact copy of the Kotomarket archive, isolated from the original.
A new clearance level slowly appears on the holopanel: write access.
For seven chapters, you've been reading someone else's memory: selecting rows, filtering, grouping, joining tables, building , and analyzing rows with . Today, for the first time, you'll write to it.
QUERY: The original is untouchable — we write only to the copy. And remember the archivist's code: every change must be deliberate, reversible, and visible. Anything else is vandalism.

What is INSERT?
INSERT adds new rows to a table.
So far, you've only looked through the Kotomarket storefront: reading users, products, orders, and order_items. Now you can add a new record to a table: a supplier, a coupon, a test product, or a row in a reporting table.
INSERT is part of — Data Manipulation Language, the language used to modify data.
Don't confuse the two:
CREATE TABLE ...
creates the structure of a table. That's .
INSERT INTO ...
adds data to an existing table. That's DML.
The basic form of INSERT
The simplest pattern looks like this:
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
For example, let's add one supplier:
INSERT INTO suppliers (id, name, country)
VALUES (1, 'CatSupply', 'United States');
You can read it almost literally:
INSERT INTO suppliers— insert a row into thesupplierstable;(id, name, country)— these are the columns we're filling;VALUES (...)— these are the values for those columns, in the same order.
Why you should list columns explicitly
A good habit is to always name the columns:
INSERT INTO suppliers (id, name, country)
VALUES (1, 'CatSupply', 'United States');
Rather than this:
INSERT INTO suppliers
VALUES (1, 'CatSupply', 'United States');
In the second version, SQL matches values to columns by the physical column order in the table.
Today, the table might look like this:
id, name, country
and the query will work.
Tomorrow, someone might add a new column, change the schema, or recreate the table with a different layout. The same INSERT could then fail or, worse, put data into the wrong columns.
QUERY calls an explicit column list a seat belt: it barely gets in the way, but it can save you when the schema changes.
Adding multiple rows
After VALUES, you can provide several tuples instead of just one:
INSERT INTO suppliers (id, name, country)
VALUES
(1, 'CatSupply', 'United States'),
(2, 'Paws & Tails', 'Canada'),
(3, 'Meow Logistics', 'Mexico');
Each pair of parentheses represents one new row.
This is more convenient than three separate INSERT statements: it avoids repetition, requires fewer trips to the database, and makes a small batch of test data easier to read.
What happens to omitted columns
You don't have to list every column in the table in an INSERT. If you omit a column, the database tries to fill it according to the table definition.
The rules are:
- If the column has a
DEFAULT, the default value is used. - If there is no
DEFAULT,NULLis inserted. - If the column is
NOT NULLand has neither a value nor aDEFAULT, the query fails with an error.
For example:
CREATE TABLE coupons (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
code VARCHAR(20) NOT NULL,
discount INT DEFAULT 10
);
INSERT INTO coupons (code)
VALUES ('CAT5');
We specified only code.
So:
- the database generates
idautomatically; discountgets the value10;codegets the value'CAT5'.
You can also write DEFAULT explicitly:
INSERT INTO coupons (code, discount)
VALUES ('CAT7', DEFAULT);
That means: “for discount, use the default value defined in the schema.”
There's also a separate form:
INSERT INTO coupons DEFAULT VALUES;
It tries to create a row entirely from default values. It works only if every required column can fill itself. In our example, it fails because code is a required column with no DEFAULT.
INSERT INTO ... SELECT
Sometimes you don't want to type new rows manually in VALUES. Instead, you want to create them from data that already exists.
For that, use this form:
INSERT INTO target_table (column1, column2, column3)
SELECT expression1, expression2, expression3
FROM source_table
WHERE ...;
For example, we'll build a separate premium-books table — you'll run this query later in the lesson.
Here, INSERT doesn't get its values from VALUES. It takes the result of the SELECT and inserts every result row into the premium_books table.
The number and order of expressions in the SELECT must match the column list in the INSERT.
INSERT INTO premium_books (id, name, price)
SELECT id, name, ROUND(price * 1.5, 2)
idgoes intoid;namegoes intoname;ROUND(price * 1.5, 2)goes intoprice.
QUERY checks this strictly: if there are three columns, the SELECT must produce three expressions.
A quick look ahead: in PostgreSQL, you can add RETURNING to an INSERT to see the rows you just wrote immediately. A later lesson in this chapter covers it in detail. For now, notice RETURNING in the demo queries: it helps you check exactly what was inserted.
products with a single INSERT: id 21 — 'Cat Roly-Poly Toy', category 'Toys', price $7.90, stock 40; id 22 — 'Teaser Wand', 'Toys', price $4.50, stock 120. The final SELECT in the starter code will show all toys.While QUERY keeps an eye on the log, let's look at the properties of INSERT that come up in interviews more often than the syntax itself.
A multi-row INSERT is atomic: all or nothing
A multi-row INSERT is one SQL statement.
If the statement succeeds, every row from VALUES is inserted into the table.
If even one row violates a , the entire statement fails, and none of the rows in that batch are saved.
For example:
CREATE TABLE suppliers (
id INT PRIMARY KEY,
name VARCHAR(80),
country VARCHAR(60)
);
INSERT INTO suppliers (id, name, country)
VALUES
(10, 'Tail & Co.', 'United States'),
(10, 'Duplicate Tail', 'United States');
Both rows try to insert id = 10.
But id is a PRIMARY KEY, so its value must be unique. The second row violates that constraint.
The result:
ERROR: duplicate key value violates unique constraint
The first row doesn't appear in the table either.
Although that row is valid on its own, it belongs to the same INSERT statement. The statement fails as a whole, so the database rolls back the entire batch.
QUERY: The archive doesn't accept half an invoice. If one capsule is defective, the whole invoice goes back to the inspection desk.
This is a basic property of SQL statements: each individual statement is atomic.
Transactions are needed when several actions, rather than one INSERT, must succeed or fail together:
INSERT ...
UPDATE ...
DELETE ...
You'll study separately later.
How a multi-row INSERT differs from several separate INSERT statements
Compare these two approaches.
One statement:
INSERT INTO suppliers (id, name, country)
VALUES
(1, 'CatSupply', 'United States'),
(1, 'Duplicate', 'United States'),
(2, 'Meow Logistics', 'Mexico');
If one row violates the PRIMARY KEY, the entire statement is rolled back.
Now compare that with three separate statements:
INSERT INTO suppliers (id, name, country)
VALUES (1, 'CatSupply', 'United States');
INSERT INTO suppliers (id, name, country)
VALUES (1, 'Duplicate', 'United States');
INSERT INTO suppliers (id, name, country)
VALUES (2, 'Meow Logistics', 'Mexico');
In mode, each statement is committed separately. The first INSERT may already be saved, the second will fail, and the third may still run if the client continues sending statements.
This distinction matters in interviews: one multi-row INSERT and many separate INSERT statements behave differently when an error occurs.
Omitted columns: DEFAULT or NULL
If a column isn't listed in the INSERT, the database doesn't “guess” a value. It follows the table schema.
For example:
CREATE TABLE supplier_notes (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
supplier_name VARCHAR(80) NOT NULL,
status VARCHAR(20) DEFAULT 'active',
comment TEXT
);
INSERT INTO supplier_notes (supplier_name)
VALUES ('CatSupply')
RETURNING id, supplier_name, status, comment;
Here's what happens:
idis generated automatically;supplier_namegets'CatSupply';statusgets itsDEFAULT, which is'active';commentgetsNULLbecause no value was provided and noDEFAULTis defined.
If you omit a required column that has no DEFAULT, you get an error:
INSERT INTO supplier_notes (status)
VALUES ('active');
Here, supplier_name is missing even though it's NOT NULL. The database can't create the row.
Why INSERT without a column list is risky
Technically, this query is valid:
INSERT INTO suppliers
VALUES (1, 'CatSupply', 'United States');
But it depends on the table's current column order.
Suppose the table changes:
ALTER TABLE suppliers
ADD COLUMN created_at DATE DEFAULT CURRENT_DATE;
Or suppose the table is recreated with the columns in a different order. The old INSERT without a column list may fail or behave unexpectedly.
It's safer to always write this:
INSERT INTO suppliers (id, name, country)
VALUES (1, 'CatSupply', 'United States');
Now the query states explicitly which value belongs in which column.
This matters especially in application code, , automated tests, and seed scripts. A month from now, you may not remember the column order, but an explicit column list is still clear to both you and the database.
INSERT, fill gift_shelf (id, name, price) with every product from products whose price is less than $15. Don't list the values manually. The final SELECT in the starter code will show what made it onto the shelf.Interview question
Interview question: A single multi-row INSERT tries to insert a batch of 100 rows, and row 57 violates a UNIQUE . What ends up in the table?
Strong answer: None of the rows from that batch. A multi-row INSERT is one SQL statement, and one statement is atomic: either all rows are applied successfully, or the entire statement is rolled back if an error occurs. That means the 56 valid rows before the conflict aren't saved either.
If you need to “insert everything that doesn't conflict,” you use other approaches: for example, INSERT ... ON CONFLICT DO NOTHING in PostgreSQL, or load the data into a and then filter out duplicates. That's a separate topic.
It's also important to distinguish one multi-row INSERT from one hundred separate INSERT statements. If you send 100 separate commands in mode, successful statements may be saved while the conflicting one fails. With the multi-row form, the entire batch fails.
INSERT statement?INSERT?INSERT INTO suppliers VALUES (...) without a column list?INSERT violates the PRIMARY KEY?
CREATE TABLE suppliers ( id INT PRIMARY KEY, name VARCHAR(80),
country VARCHAR(60)
);
INSERT INTO suppliers (id, name, country)
VALUES (1, 'CatSupply', 'United States')
RETURNING id, name, country;