# INSERT

> Add rows with INSERT, see what happens to columns you leave out, and read the errors when a new row breaks a rule.

Subject: SQL | Lesson 29 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/insert-rows
Previous: [Ranking & Running Totals](https://codewithanimation.in/sql/ranking-and-running-totals.md) | Next: [UPDATE & DELETE](https://codewithanimation.in/sql/update-and-delete.md)

## INSERT adds rows

`INSERT INTO table (columns) VALUES (values)` adds a new row. List the columns you are filling, then the values in the same order. The column list can be in any order, as long as the values match it.

You can add several rows in one statement by repeating the brackets after `VALUES`, and you can copy rows from a query with `INSERT INTO ... SELECT ...`.

## How data gets in

Every row in every table started as an `INSERT`. Writing the column list out, instead of relying on the table's column order, keeps your statement working when someone later adds a column.

## Left-out columns and rejected rows

```sql
INSERT INTO members (name, email)
VALUES ('Dara', 'dara@mail.test');
```

The `members` table has an `id` that numbers itself. Because `id` is not in the column list, it takes its default, the next number from its sequence, so Dara gets 4. A column with no default that you leave out becomes `NULL`.

PostgreSQL answers with a command tag: `INSERT 0 1` means one row was inserted. The 0 is a leftover and is always 0.

If a row breaks a rule, the whole statement fails and nothing is added. A repeated `id` or `email` gives `duplicate key value violates unique constraint`, and a NULL in a NOT NULL column gives `null value in column "name" ... violates not-null constraint`.

## Common mistake: Gaps in the ids

The number for `id` is taken from the sequence before the row is checked. If the insert then fails, that number is not given back. The next successful insert skips it, so ids can have gaps, and that is normal.

Never rely on ids being gapless or in order, and never use them as a count of rows. If you need to know how many rows exist, use `count(*)`.

Do not skip the column list. `INSERT INTO members VALUES (...)` depends on the exact column order, and breaks if the table changes.

## Tip: Always name your columns

Write `INSERT INTO table (a, b) VALUES (...)`, and insert many rows in one statement when you can. Let the database fill the id and let it tell you which one it chose, which the next lessons show with `RETURNING`.

## Try it: Insert good and bad rows

Run a few good inserts, then try the repeated id, the repeated email and the missing name. Watch the next id after each one.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/insert-rows

## Check yourself

### Question 1

`INSERT INTO members (name) VALUES ('Eli')` is run. The table also has an `email` column with no default. What is Eli's email?

- A. NULL
- B. An empty text
- C. The statement fails because email is missing

Correct answer: A

A column that is not in the list gets its default value, or NULL when there is no default.

- Why not B: A column with no default becomes NULL, not an empty text.
- Why not C: A column can be left out. It only fails if the column is NOT NULL and has no default.

### Question 2

One statement inserts two rows, and the second row repeats an existing primary key. How many rows are added?

- A. None, because the whole statement fails
- B. One, the first row
- C. Two, and the duplicate is ignored

Correct answer: A

If any row breaks a rule, PostgreSQL rejects the statement and changes nothing.

- Why not B: A statement is all or nothing. A rejected row cancels the whole statement.
- Why not C: Duplicates are only ignored when you write ON CONFLICT DO NOTHING.

### Question 3

A failed INSERT reserved id 4 from the sequence. The next successful insert gets which id?

- A. 5, because id 4 is not given back
- B. 4, because the failed row was never stored
- C. NULL

Correct answer: A

The manual says a number taken from a sequence is not reclaimed, so gaps can appear.

- Why not B: A sequence value is never reused, even when the insert fails.
- Why not C: The sequence always supplies a number. It never hands out NULL.

## Remember this

- INSERT INTO table (columns) VALUES (...) adds rows, and one statement can add several.
- A left-out column gets its default, or NULL when it has none.
- A row that breaks a rule makes the whole statement fail, and nothing is added.
- Sequence numbers are not given back, so ids can have gaps. Always name the columns.
