INSERT INTO

Updated · 9 min read
easyPostgreSQL 18sqlinsertreturningidentitychanging-data

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 NULL if 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

SQL
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
PartWhat it does
INSERT INTO table_nameSays 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:

SQL▶ Try it
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;
Output
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.

You give the values; the database adds the row and generates payment_id 8

Several Rows at Once

List several value groups, separated by commas, to insert several rows in one statement. Each group is one row:

SQL▶ Try it
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;
Output
 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:

SQL▶ Try it
INSERT INTO enrollments (student_id, course_id, enrolled_on, status)
VALUES (103, 6, '2026-01-05', 'active')
RETURNING *;
Output
 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 listWith 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 orderWorks whatever the order
Must include every column, even generated onesLeave 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:

SQL▶ Try it
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');
Output
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:

SQL▶ Try it
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;
Output
 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:

SQL▶ Try it
INSERT INTO payments (student_id, course_id, amount_usd, paid_on)
VALUES (102, 4, 200.00, '2026-01-05');
Output
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).
Generated id, NULL for an optional column, and an error for a required one

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:

RuleExampleError
Unique valueA second course titled Java Basicsduplicate key value violates unique constraint "courses_title_key"
CHECKAn enrollment with status pendingviolates check constraint "enrollments_status_check"
NOT NULLA payment with no due_onnull value in column "due_on" ... violates not-null constraint
Foreign keyA payment for a course the student isn't enrolled inviolates foreign key constraint "payments_student_id_course_id_fkey"
SQL▶ Try it
INSERT INTO enrollments (student_id, course_id, enrolled_on, status)
VALUES (103, 6, '2026-01-05', 'pending');
Output
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.

Each rule is a gate; a row that fails any gate isn't added

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
SQL▶ Try it
INSERT INTO courses (title, technology, level, fee_usd)
VALUES ('Python Basics', 'Python', 'Beginner', 400.00)
RETURNING course_id, title, prerequisite_id;
Output
 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.)

Quick check: INSERT INTO

3 questions
1Why should you always name the columns in an INSERT?
2You insert a payment without a method. What is stored in method?
3What does RETURNING payment_id do in an INSERT?

Key Takeaways

You want toWrite
Add one rowINSERT INTO payments (student_id, ...) VALUES (102, ...);
Add several rowsVALUES (...), (...);
See the new row... RETURNING *;
Use the defaultLeave the column out

Was this tutorial helpful?

Lesson 21 of 115