SQL INSERT
What you will learn
- How to add a row with
INSERT INTO ... VALUES - Why you should name the columns
- How to add several rows at once and copy rows with
INSERT ... SELECT - What happens to columns you leave out, and what the errors mean
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). The changes happen only in your browser's copy of the data; Reset data puts everything back, so you cannot damage anything. Run the cells from top to bottom.
1. INSERT one row
INSERT INTO table (columns) VALUES (values) adds one row. The values are matched to the columns by position, so list them in the same order:
INSERT INTO customers (first_name, last_name, email, phone, city, country, joined_on)
VALUES ('Mina', 'Rahman', 'mina.rahman@example.com', '+44 7000 111222', 'Leeds', 'United Kingdom', '2026-10-01');
SELECT customer_id, first_name, last_name, city, is_active FROM customers ORDER BY customer_id DESC LIMIT 2;
customer_id first_name last_name city is_active
----------- ---------- --------- ----- ---------
421 Mina Rahman Leeds 1
420 Lara Singh Leeds 1
(2 rows)
INSERT INTO customers (full_name, birth_date, city, joined_on)
VALUES ('Mina Rahman', '1992-04-17', 'Lima', '2026-10-01');
SELECT customer_id, full_name, city, risk_level FROM customers ORDER BY customer_id DESC LIMIT 2;
customer_id full_name city risk_level
----------- ----------- ------------ ----------
301 Mina Rahman Lima normal
300 Olga Jensen Porto Alegre low
(2 rows)
INSERT INTO students (first_name, last_name, birth_date, enrolled_year, email, guardian_name)
VALUES ('Mina', 'Rahman', '2013-04-17', 2026, 'mina.rahman@student.example', 'Sara Rahman');
SELECT student_id, first_name, last_name, enrolled_year FROM students ORDER BY student_id DESC LIMIT 2;
student_id first_name last_name enrolled_year
---------- ---------- --------- -------------
361 Mina Rahman 2026
360 Eva Alvarez 2026
(2 rows)
The key column (customer_id, student_id) was not given: the database chose the next number (lesson 2.4 of Database Fundamentals). The SELECT after the insert proves that the new row exists.
2. Name the columns
You may leave out the column list (INSERT INTO table VALUES (...)), but then you must supply every column, in table order. That breaks the day someone adds a column. Always name the columns, as above. It also lets you skip the ones you do not need.
3. Columns you leave out
A column you leave out gets its default value, or NULL if it has none. Look at what the new row got:
is_active has a default of 1; phone has none, so it is NULL:
INSERT INTO customers (first_name, last_name, email, city, country, joined_on)
VALUES ('Omar', 'Said', 'omar.said@example.com', 'Cairo', 'Egypt', '2026-10-02');
SELECT first_name, phone, is_active FROM customers WHERE email = 'omar.said@example.com';
first_name phone is_active
---------- ----- ---------
Omar NULL 1
(1 row)
risk_level has a default of 'normal':
INSERT INTO customers (full_name, birth_date, city, joined_on)
VALUES ('Omar Said', '1988-11-03', 'Cairo', '2026-10-02');
SELECT full_name, risk_level FROM customers WHERE full_name = 'Omar Said';
full_name risk_level
--------- ----------
Omar Said normal
(1 row)
guardian_name has no default, so it is NULL:
INSERT INTO students (first_name, last_name, birth_date, enrolled_year, email)
VALUES ('Omar', 'Said', '2012-11-03', 2026, 'omar.said@student.example');
SELECT first_name, email, guardian_name FROM students WHERE email = 'omar.said@student.example';
first_name email guardian_name
---------- ------------------------- -------------
Omar omar.said@student.example NULL
(1 row)
A column that is NOT NULL and has no default must be given, or the insert fails (section 5).
4. Several rows at once, and copying rows
List several value groups, separated by commas, to add many rows in one statement:
INSERT INTO categories (name, parent_id) VALUES ('Pets', NULL), ('Books', NULL), ('Music', NULL);
SELECT category_id, name FROM categories ORDER BY category_id DESC LIMIT 3;
category_id name
----------- -----
13 Music
12 Books
11 Pets
(3 rows)
INSERT INTO branches (name, city) VALUES ('Hilltop', 'Oslo'), ('Lakeside', 'Zurich'), ('Seaview', 'Lisbon');
SELECT branch_id, name, city FROM branches ORDER BY branch_id DESC LIMIT 3;
branch_id name city
--------- -------- ------
9 Seaview Lisbon
8 Lakeside Zurich
7 Hilltop Oslo
(3 rows)
INSERT INTO departments (name, building) VALUES ('Music', 'North'), ('Sport', 'Gym'), ('Drama', NULL);
SELECT department_id, name, building FROM departments ORDER BY department_id DESC LIMIT 3;
department_id name building
------------- ----- --------
9 Drama NULL
8 Sport Gym
7 Music North
(3 rows)
INSERT INTO ... SELECT copies rows from a query into a table. Create a small table of your own and fill it from the data:
CREATE TABLE big_orders (order_id INTEGER PRIMARY KEY, order_date TEXT, status TEXT);
INSERT INTO big_orders (order_id, order_date, status)
SELECT order_id, order_date, status FROM orders WHERE order_id <= 5;
SELECT * FROM big_orders ORDER BY order_id;
order_id order_date status
-------- ---------- ---------
1 2025-04-23 delivered
2 2024-01-21 delivered
3 2025-05-03 delivered
4 2026-08-18 paid
5 2024-05-26 delivered
(5 rows)
CREATE TABLE big_accounts (account_id INTEGER PRIMARY KEY, account_type TEXT, balance_cents INTEGER);
INSERT INTO big_accounts (account_id, account_type, balance_cents)
SELECT account_id, account_type, balance_cents FROM accounts WHERE balance_cents >= 450000;
SELECT * FROM big_accounts ORDER BY account_id;
account_id account_type balance_cents
---------- ------------ -------------
398 savings 502080
(1 row)
CREATE TABLE heavy_courses (course_id INTEGER PRIMARY KEY, code TEXT, credits INTEGER);
INSERT INTO heavy_courses (course_id, code, credits)
SELECT course_id, code, credits FROM courses WHERE credits >= 4;
SELECT * FROM heavy_courses ORDER BY course_id;
course_id code credits
--------- ------- -------
1 MATH101 4
3 MATH301 4
4 SCI101 4
5 SCI201 4
6 SCI202 4
18 CS201 4
19 CS301 4
(7 rows)
5. When a rule is broken
The database refuses an insert that breaks a rule of the table, and nothing is added. Each message names the rule:
A repeated email (the column is UNIQUE):
INSERT INTO customers (first_name, last_name, email, city, country, joined_on)
VALUES ('Mina', 'Again', 'mina.rahman@example.com', 'Leeds', 'United Kingdom', '2026-10-03');
IntegrityError: UNIQUE constraint failed: customers.email
A missing required value (NOT NULL):
INSERT INTO customers (first_name, last_name, city, country, joined_on)
VALUES ('No', 'Email', 'Leeds', 'United Kingdom', '2026-10-03');
IntegrityError: NOT NULL constraint failed: customers.email
A value that fails a CHECK, here a price that must be above zero:
INSERT INTO products (sku, name, category_id, supplier_id, price, cost, added_on)
VALUES ('SKU-9999', 'Free thing', 1, 1, 0, 1, '2026-10-03');
IntegrityError: CHECK constraint failed: price > 0
A link to a row that does not exist (foreign key):
INSERT INTO orders (customer_id, order_date, status, ship_city)
VALUES (999999, '2026-10-03', 'pending', 'Leeds');
IntegrityError: FOREIGN KEY constraint failed
A missing required value (NOT NULL):
INSERT INTO customers (full_name, city, joined_on) VALUES ('No Birthday', 'Lima', '2026-10-03');
IntegrityError: NOT NULL constraint failed: customers.birth_date
A value that fails a CHECK, here a risk level that is not allowed:
INSERT INTO customers (full_name, birth_date, city, joined_on, risk_level)
VALUES ('Odd Risk', '1990-01-01', 'Lima', '2026-10-03', 'extreme');
IntegrityError: CHECK constraint failed: risk_level IN ('low', 'normal', 'high')
A repeated value in a UNIQUE column (branch names are unique):
INSERT INTO branches (name, city) VALUES ('Central', 'Paris');
IntegrityError: UNIQUE constraint failed: branches.name
A link to a row that does not exist (foreign key):
INSERT INTO accounts (customer_id, branch_id, account_type, opened_on, balance_cents)
VALUES (999999, 1, 'savings', '2026-10-03', 0);
IntegrityError: FOREIGN KEY constraint failed
A repeated email (the column is UNIQUE):
INSERT INTO students (first_name, last_name, birth_date, enrolled_year, email)
VALUES ('Mina', 'Again', '2013-01-01', 2026, 'mina.rahman@student.example');
IntegrityError: UNIQUE constraint failed: students.email
A missing required value (NOT NULL):
INSERT INTO students (first_name, last_name, enrolled_year) VALUES ('No', 'Birthday', 2026);
IntegrityError: NOT NULL constraint failed: students.birth_date
A value that fails a CHECK, here a grade above 100:
INSERT INTO enrollments (student_id, section_id, enrolled_on, grade) VALUES (1, 1, '2026-10-03', 150);
IntegrityError: CHECK constraint failed: grade BETWEEN 0 AND 100
A link to a row that does not exist (foreign key):
INSERT INTO enrollments (student_id, section_id, enrolled_on) VALUES (999999, 1, '2026-10-03');
IntegrityError: FOREIGN KEY constraint failed
These rules protect the data (Database Fundamentals, lessons 2.7 and 3.5). When an insert fails, read the message: it names the rule and the column.
Worked example: add a customer safely
| Step | Do |
|---|---|
| 1 | look at the table's columns and rules (lesson 1.1) |
| 2 | list the required columns (NOT NULL, no default) |
| 3 | write INSERT INTO table (those columns) VALUES (...) |
| 4 | run it, then SELECT the new row to check it |
| 5 | if it fails, read which rule was broken and fix the value |
The tables are from the sample databases.
Common mistakes
- No column list, so a new column breaks the statement.
- Values in a different order from the columns.
- Text without quotes, or a wrong quote style.
- Forgetting a required column.
- Running the same insert twice and adding the row twice (or hitting a
UNIQUEerror).
Key takeaways
INSERT INTO t (a, b) VALUES (1, 2)adds a row; name the columns.- Omitted columns get their default, or
NULL. - Several value groups add several rows;
INSERT ... SELECTcopies rows from a query. - The database refuses a row that breaks a rule, and adds nothing.
Practice
1. Write an insert that adds a product with the columns name and price.
Show answer
INSERT INTO products (name, price) VALUES ('Lamp', 25.0); (a real table may need more columns).
2. Why name the columns in an INSERT?
Show answer
So the statement still works when the table gets a new column, and so the values cannot be matched to the wrong columns.
3. What value does an omitted column get?
Show answer
Its default value, or NULL if it has none.
4. How do you add three rows in one statement?
Show answer
List three value groups after VALUES, separated by commas.
5. What does UNIQUE constraint failed: customers.email mean?
Show answer
A customer with that email already exists, and the column must be unique, so the insert was refused.
6. What does INSERT INTO t2 SELECT ... FROM t1 do?
Show answer
It copies the rows returned by the query into t2.