So far you have only read data. The INSERT INTO statement adds new rows to a table: a new customer signs up, a new product arrives, an order is placed.
SELECT so you can see the effect. Your changes vanish on the next run, so experiment as much as you like.Syntax
The recommended form names the columns you are filling, then gives the values in the same order:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Inserting one row
A new customer from Machakos joins Skope Shop:
INSERT INTO customers (customer_id, name, contact, city, country, joined)
VALUES (13, 'Machakos Hardware', 'Mutua Musyoka', 'Machakos', 'Kenya', '2024-07-01');
SELECT * FROM customers WHERE customer_id = 13;Try it Yourself »Text and dates go in single quotes; numbers do not. The values must match the column list one for one: the first value goes into the first named column, and so on.
Leaving columns out
You don't have to list every column. Any column you leave out gets its default value, which is NULL unless the table defines something else. Here we don't know the contact person or join date yet:
INSERT INTO customers (customer_id, name, city, country)
VALUES (13, 'Kitale Agrovet', 'Kitale', 'Kenya');
SELECT * FROM customers WHERE city = 'Kitale';Try it Yourself »The contact and joined columns are NULL.
Automatic IDs
In the Skope Shop tables, each ID column is declared INTEGER PRIMARY KEY. In SQLite that means if you leave it out, the database picks the next number for you:
INSERT INTO products (name, category, price, stock)
VALUES ('Laptop Bag', 'Accessories', 3200, 50);
SELECT * FROM products ORDER BY product_id DESC LIMIT 3;Try it Yourself »The new product gets ID 13 automatically.
AUTO_INCREMENT in MySQL, SERIAL or GENERATED ... AS IDENTITY in PostgreSQL, IDENTITY in SQL Server, and INTEGER PRIMARY KEY (optionally with AUTOINCREMENT) in SQLite.Inserting several rows at once
List several sets of values separated by commas. A new order with two product lines:
INSERT INTO orders (order_id, customer_id, order_date, status)
VALUES (113, 12, '2024-06-20', 'pending');
INSERT INTO order_items (order_id, product_id, quantity)
VALUES (113, 9, 20),
(113, 10, 2);
SELECT o.order_id, o.status, p.name, oi.quantity
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.order_id
JOIN products AS p ON p.product_id = oi.product_id
WHERE o.order_id = 113;Try it Yourself »Multi-row VALUES is supported by SQLite, MySQL, PostgreSQL and SQL Server 2008+. It is faster than many separate statements.
Inserting without a column list
You can skip the column list entirely if you give a value for every column in the exact order they were created:
INSERT INTO customers
VALUES (13, 'Kericho Tea Co', 'Chebet Langat', 'Kericho', 'Kenya', '2024-07-03');
Copying rows with INSERT ... SELECT
Instead of VALUES, you can insert the result of a query. Here we create an empty archived_orders table with the same columns, then copy the cancelled orders into it:
CREATE TABLE archived_orders (
order_id INTEGER,
customer_id INTEGER,
order_date TEXT,
status TEXT
);
INSERT INTO archived_orders (order_id, customer_id, order_date, status)
SELECT order_id, customer_id, order_date, status
FROM orders
WHERE status = 'cancelled';
SELECT * FROM archived_orders;Try it Yourself »You will learn CREATE TABLE properly in CREATE TABLE.
When an insert fails
The database refuses rows that break the table's rules. Try giving a new customer an ID that is already taken, or leaving out name, which is declared NOT NULL. You will see an error and nothing is inserted.
Complete the statement that adds a new product.
products (name, category, price, stock) ('HDMI Cable', 'Accessories', 600, 80);
SKOPE