INSERT INTO
INSERT INTO adds new rows to a table. When Advitha makes her first payment, or the program adds a Python course, an INSERT records it. This lesson covers adding one row and several rows, why you should always name the columns, what the database fills in for you, how to see the new row straight away with RETURNING, and the rules that refuse a bad row. Every Try it run starts from a fresh copy of the Fee Tracker, so nothing you insert here is kept.
In One Minute
INSERT INTO table (columns) VALUES (values);adds a row; list several value groups to add several rows.- Always name the columns. Then the statement keeps working if the table's columns change.
- Leave out a column to get its default: a generated id, or
NULLif the column allows it. RETURNING(PostgreSQL) shows the inserted row, including values the database generated.- The table's rules (types,
NOT NULL,CHECK, unique values, foreign keys) refuse a bad row with an error.
The Basics
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);| Part | What it does |
|---|---|
INSERT INTO table_name | Says which table gets the row |
(column1, column2, ...) | The columns you're giving values for, in the order you choose |
VALUES (value1, value2, ...) | The values, in the same order as the column list |
Advitha has enrolled in SQL Basics but hasn't paid yet. Here is her first payment, followed by a SELECT that shows it arrived:
INSERT INTO payments (student_id, course_id, amount_usd, due_on, paid_on, method)
VALUES (102, 4, 200.00, '2026-01-05', '2026-01-05', 'card');
SELECT payment_id, student_id, course_id, amount_usd, paid_on
FROM payments
WHERE student_id = 102;INSERT 0 1
payment_id | student_id | course_id | amount_usd | paid_on
------------+------------+-----------+------------+------------
8 | 102 | 4 | 200.00 | 2026-01-05
(1 row)INSERT 0 1 means one row was added. (The 0 is a leftover from older PostgreSQL versions and is always 0.) Notice that you didn't give a payment_id: the database generated 8, the next number after the seven existing payments.
Several Rows at Once
List several value groups, separated by commas, to insert several rows in one statement. Each group is one row:
INSERT INTO payments (student_id, course_id, amount_usd, due_on, paid_on, method)
VALUES (102, 1, 225.00, '2026-01-05', '2026-01-05', 'card'),
(102, 4, 200.00, '2026-01-05', '2026-01-05', 'check')
RETURNING payment_id, course_id, amount_usd; payment_id | course_id | amount_usd
------------+-----------+------------
8 | 1 | 225.00
9 | 4 | 200.00
(2 rows)One statement is faster than many single-row inserts, and if any row breaks a rule, none of them are added: the statement succeeds or fails as a whole.
Seeing the New Row: RETURNING
RETURNING makes an INSERT hand back the rows it added, including values the database filled in. That's how an app learns the id of the payment it just recorded, without a second query. It's PostgreSQL only (SQLite and Oracle have similar clauses; SQL Server uses OUTPUT). RETURNING * returns every column:
INSERT INTO enrollments (student_id, course_id, enrolled_on, status)
VALUES (103, 6, '2026-01-05', 'active')
RETURNING *; student_id | course_id | enrolled_on | status
------------+-----------+-------------+--------
103 | 6 | 2026-01-05 | active
(1 row)Navika is now enrolled in Git Basics, the course that had no students until now.
Always Name the Columns
You can leave out the column list, but you shouldn't. Without it, you must give a value for every column, in the table's exact order, and the statement breaks or puts values in the wrong place as soon as someone adds or reorders a column. With the list, the statement says exactly what goes where:
| Without a column list | With a column list |
|---|---|
INSERT INTO payments VALUES (8, 102, 4, 200.00, ...) | INSERT INTO payments (student_id, course_id, amount_usd, ...) VALUES (102, 4, 200.00, ...) |
| Depends on the table's column order | Works whatever the order |
| Must include every column, even generated ones | Leave out what the database fills in |
Here it is happening. The first insert chooses payment_id 8 by hand; the second lets the database choose, and it picks 8 too:
INSERT INTO payments (payment_id, student_id, course_id, amount_usd, due_on, paid_on)
VALUES (8, 102, 4, 200.00, '2026-01-05', '2026-01-05');
INSERT INTO payments (student_id, course_id, amount_usd, due_on, paid_on)
VALUES (102, 4, 200.00, '2026-01-05', '2026-01-05');INSERT 0 1
ERROR: duplicate key value violates unique constraint "payments_pkey"
DETAIL: Key (payment_id)=(8) already exists.Leaving Columns Out: Defaults
A column you leave out gets its default value. For a generated id, that's the next number; for most other columns, it's NULL, if the column allows it. Here the method is left out, so it's stored as NULL:
INSERT INTO payments (student_id, course_id, amount_usd, due_on, paid_on)
VALUES (102, 4, 200.00, '2026-01-05', '2026-01-05')
RETURNING payment_id, method; payment_id | method
------------+--------
8 | NULL
(1 row)Leave out a column that requires a value and the row is refused. due_on is NOT NULL:
INSERT INTO payments (student_id, course_id, amount_usd, paid_on)
VALUES (102, 4, 200.00, '2026-01-05');ERROR: null value in column "due_on" of relation "payments" violates not-null constraint
DETAIL: Failing row contains (8, 102, 4, 200.00, null, 2026-01-05, null).The Rules That Refuse a Bad Row
Every rule on the table is checked before the row is added. If any fails, the row isn't added and you get an error that names the rule:
| Rule | Example | Error |
|---|---|---|
| Unique value | A second course titled Java Basics | duplicate key value violates unique constraint "courses_title_key" |
CHECK | An enrollment with status pending | violates check constraint "enrollments_status_check" |
NOT NULL | A payment with no due_on | null value in column "due_on" ... violates not-null constraint |
| Foreign key | A payment for a course the student isn't enrolled in | violates foreign key constraint "payments_student_id_course_id_fkey" |
INSERT INTO enrollments (student_id, course_id, enrolled_on, status)
VALUES (103, 6, '2026-01-05', 'pending');ERROR: new row for relation "enrollments" violates check constraint "enrollments_status_check"
DETAIL: Failing row contains (103, 6, 2026-01-05, pending).The DETAIL line shows the row that failed. You'll write rules like these yourself in Constraints.
Common Mistakes
Leaving out the column list. The statement depends on the table's column order. Name the columns.
Values in a different order from the columns. (student_id, course_id) with VALUES (4, 102) swaps them; the foreign key may catch it, or it may not. Keep the two lists in the same order.
Inserting your own value for a generated id. It can cause duplicate-key errors later. Let the database choose.
Quoting numbers or forgetting quotes on text and dates. Numbers go bare (200.00); text and dates go in single quotes ('card', '2026-01-05').
Practice
Challenge: The program adds a new course: "Python Basics", technology "Python", level "Beginner", fee $400.00, with no prerequisite. Insert it, naming the columns, and use RETURNING to show the course_id the database generated, the title and the prerequisite_id.
Show solution
INSERT INTO courses (title, technology, level, fee_usd)
VALUES ('Python Basics', 'Python', 'Beginner', 400.00)
RETURNING course_id, title, prerequisite_id; course_id | title | prerequisite_id
-----------+---------------+-----------------
7 | Python Basics | NULL
(1 row)The database generated course_id 7, and prerequisite_id is NULL because you left it out. (In Try it the course disappears at the end of the run.)
Key Takeaways
| You want to | Write |
|---|---|
| Add one row | INSERT INTO payments (student_id, ...) VALUES (102, ...); |
| Add several rows | VALUES (...), (...); |
| See the new row | ... RETURNING *; |
| Use the default | Leave the column out |
Was this tutorial helpful?