SQL Tutorial · Changing Data and Tables · Lesson 27 of 36

SQL INSERT

Add rows to a table with INSERT INTO.

INSERT INTO

List the columns, then the values in the same order:

Example 1Try it yourself
INSERT INTO customers (name, email, city, country, signup_date)
VALUES ('Hedy Lamarr', '[email protected]', 'Vienna', 'Austria', '2026-10-12');
SELECT customer_id, name, country FROM customers WHERE name = 'Hedy Lamarr';

Output

customer_id | name | country
11 | Hedy Lamarr | Austria

Columns you leave out get their DEFAULT value (or NULL). An INTEGER PRIMARY KEY is generated automatically.

Multiple rows and INSERT ... SELECT

Example 2Try it yourself
INSERT INTO categories (name) VALUES ('Garden'), ('Sports');
INSERT INTO products (name, category_id, price)
SELECT 'Gift card ' || name, category_id, 25 FROM categories WHERE name IN ('Garden', 'Sports');
SELECT name, price, stock FROM products WHERE name LIKE 'Gift card%';

Output

name | price | stock
Gift card Garden | 25 | 0
Gift card Sports | 25 | 0

Upsert

"Insert, or update if it already exists" uses ON CONFLICT in SQLite and PostgreSQL (ON DUPLICATE KEY UPDATE in MySQL):

Example 3Try it yourself
INSERT INTO categories (category_id, name) VALUES (4, 'Toys & Games')
ON CONFLICT (category_id) DO UPDATE SET name = excluded.name;
SELECT * FROM categories;

Output

category_id | name
1 | Books
2 | Electronics
3 | Home
4 | Toys & Games

Test yourself

What value does a column get if you leave it out of an INSERT?

  1. Zero
  2. An empty string
  3. Its DEFAULT value, or NULL
  4. The previous row's value
Show answer

Answer: Its DEFAULT value, or NULL