TExcelTAD
Home › SQL › SQL INSERT INTO

SQL INSERT INTO

INSERT INTO adds new rows to a table.

sql
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

List the columns you are filling, then give the values in the same order. Columns you leave out get their default value — usually NULL, or an automatically generated id for an INTEGER PRIMARY KEY.

Inserting one row

A new customer signs up:

sql · try it yourself
INSERT INTO customers (name, contact, email, city, country, joined)
VALUES ('Harbor Deli', 'Sam Lee', 'sam.lee@example.com', 'Seattle', 'USA', '2025-10-14');

SELECT customer_id, name, city, joined
FROM customers
WHERE customer_id > 23
ORDER BY customer_id;
1 row affected.

customer_id | name        | city      | joined
------------+-------------+-----------+-----------
24          | Hugo Berg   | Stockholm | 2024-12-27
25          | Elif Yilmaz | Toronto   | 2024-01-07
26          | Harbor Deli | Seattle   | 2025-10-14
(3 rows)

We did not provide customer_id; SQLite assigned the next number, 26.

Inserting several rows at once

One INSERT can add many rows — separate the value lists with commas:

sql · try it yourself
INSERT INTO categories (name, description) VALUES
  ('Pet Supplies', 'Food and toys for pets'),
  ('Garden', 'Seeds, tools and pots'),
  ('Toys', NULL);

SELECT * FROM categories WHERE category_id > 7 ORDER BY category_id;
3 rows affected.

category_id | name         | description
------------+--------------+------------------------------
8           | Books        | Programming and cooking books
9           | Pet Supplies | Food and toys for pets
10          | Garden       | Seeds, tools and pots
11          | Toys         | NULL
(4 rows)

The message 3 rows affected. confirms that all three rows were inserted.

Omitting the column list

If you supply a value for every column in table order, the column list may be omitted:

sql
INSERT INTO categories VALUES (20, 'Stationery', 'Pens and notebooks');

This is fragile: when someone adds a column to the table later, the statement breaks or puts values in the wrong place. Always name the columns in real code.

Leaving columns empty

Columns that are not listed are filled with their default. In products, stock has DEFAULT 0; unit has no default, so it becomes NULL:

sql · try it yourself
INSERT INTO products (name, category_id, supplier_id, price)
VALUES ('Oat Milk', 3, 3, 2.3);

SELECT product_id, name, unit, price, stock
FROM products
WHERE name = 'Oat Milk';
1 row affected.

product_id | name     | unit | price | stock
-----------+----------+------+-------+------
31         | Oat Milk | NULL | 2.3   | 0
(1 row)

When an insert fails

Constraints protect the data. name is NOT NULL in products, and customer_id must be unique:

sql · try it yourself
INSERT INTO products (category_id, price) VALUES (1, 9.99);
Error: NOT NULL constraint failed: products.name
sql · try it yourself
INSERT INTO customers (customer_id, name) VALUES (1, 'Duplicate Cafe');
Error: UNIQUE constraint failed: customers.customer_id

Getting the new id back

SQLite (like PostgreSQL) supports RETURNING, which hands back columns of the inserted rows — handy for learning the generated id:

sql · try it yourself
INSERT INTO suppliers (name, city, country)
VALUES ('Nordic Berries', 'Helsinki', 'Finland')
RETURNING supplier_id, name;
supplier_id | name
------------+---------------
9           | Nordic Berries
(1 row)
Database Get generated id
SQLite (3.35+), PostgreSQL INSERT … RETURNING id
SQLite (any) SELECT last_insert_rowid()
MySQL SELECT LAST_INSERT_ID()
SQL Server OUTPUT inserted.id or SCOPE_IDENTITY()

Test yourself

What happens to a column you leave out of an INSERT column list?

  1. The insert fails
  2. It gets its default value (often NULL)
  3. It copies the previous row's value
  4. It is set to 0

How do you insert three rows in one statement?

  1. Three INSERT keywords in one line
  2. VALUES (...), (...), (...)
  3. INSERT MANY
  4. It is not possible

Why should you always list the columns in an INSERT?

  1. It is faster
  2. It keeps working if the table gets new columns or a different order
  3. It is required by SQLite
  4. It avoids NULLs