SQL INSERT INTO
INSERT INTO adds new rows to a table. Name the columns you’re filling, then give the values in the same order.
Updated December 1, 2026
Syntax
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Insert one row
Add a new drink, then check the menu.
INSERT INTO drinks (id, name, category, price, calories, caffeinated)
VALUES (11, 'Iced Tea', 'tea', 3.75, 90, 1);
Check the result:
SELECT id, name, price
FROM drinks
WHERE id >= 9;
| id | name | price |
|---|---|---|
| 9 | Hot Chocolate | 4 |
| 10 | Lemonade | 3.5 |
| 11 | Iced Tea | 3.75 |
Insert several rows at once
Separate each row’s values with a comma. One statement is faster than many single inserts.
INSERT INTO customers (id, name, city, joined)
VALUES (9, 'Ivy', 'Boston', '2024-07-10'),
(10, 'Jon', 'Austin', '2024-07-11');
Check the result:
SELECT id, name, city
FROM customers
WHERE id >= 8;
| id | name | city |
|---|---|---|
| 8 | Hana | Austin |
| 9 | Ivy | Boston |
| 10 | Jon | Austin |
Insert from a SELECT
Copy rows from a query instead of typing values. Here, everyone in Engineering becomes a new signup record.
INSERT INTO signups (email, signed_up)
SELECT lower(replace(name, ' ', '.')) || '@acme.test', '2026-01-01'
FROM employees
WHERE department_id = 1;
Check the result:
SELECT email, signed_up
FROM signups
WHERE signed_up = '2026-01-01';
| signed_up | |
|---|---|
| [email protected] | 2026-01-01 |
| [email protected] | 2026-01-01 |
| [email protected] | 2026-01-01 |
| [email protected] | 2026-01-01 |
| [email protected] | 2026-01-01 |
Common mistakes
- Leaving out the column list.
INSERT INTO drinks VALUES (...)breaks as soon as a column is added. - Values in a different order than the columns, which silently puts data in the wrong place.
- Inserting an id that already exists, which fails on a primary key. Let the database generate ids where you can.
Practice INSERT INTO interactively
Write real queries in your browser and get instant feedback in the lesson “INSERT” (Unit 9). New learners can start free from the first lesson.
Frequently asked questions
How do I get the id of the row I just inserted?
RETURNING id in PostgreSQL and SQLite 3.35+, LAST_INSERT_ID() in MySQL, OUTPUT inserted.id or SCOPE_IDENTITY() in SQL Server.
How do I insert only if the row doesn’t exist?
Use INSERT ... ON CONFLICT DO NOTHING in PostgreSQL and SQLite, or INSERT IGNORE in MySQL.