We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
INSERT - Adding Data
The INSERT statement adds new rows to a table.
Basic Syntax
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Inserting a Single Row
Your sandbox has a products table. Let's add a new product:
INSERT INTO products (name, category, price, in_stock)
VALUES ('Keyboard', 'Electronics', 79.99, true);
After inserting, verify with:
SELECT * FROM products;
Inserting Multiple Rows
Add several rows at once by separating value sets with commas:
INSERT INTO products (name, category, price, in_stock, quantity)
VALUES
('Webcam', 'Electronics', 59.99, true, 30),
('Notebook', 'Stationery', 4.99, true, 200),
('Pen Set', 'Stationery', 12.99, true, 150);
RETURNING - Get Back Inserted Data
PostgreSQL can return the inserted data (including auto-generated values):
INSERT INTO products (name, category, price)
VALUES ('USB Cable', 'Electronics', 9.99)
RETURNING id, name, created_at;
This is very useful for getting the auto-generated id of a new row.
Default Values
Columns with default values can be omitted:
-- in_stock defaults to true, quantity defaults to 0
INSERT INTO products (name, category, price)
VALUES ('Headphones', 'Electronics', 149.99);
Or explicitly use DEFAULT:
INSERT INTO products (name, category, price, quantity)
VALUES ('Tablet', 'Electronics', 399.99, DEFAULT);
Try It Yourself
- Insert a new product: "Standing Desk", category "Furniture", price 599.99
- Insert 3 products at once in a single statement
- Insert a product and use RETURNING to see its generated id
- Check your products table to see everything you've added!
SQL Editor
Ctrl+Enter to run
Results
Run a query to see results here
Tables: