SQL INSERT INTO
INSERT adds new rows to a table. List the columns, then the values in the same order. You can insert several rows at once by separating value lists with commas.
In our editor every change is rolled back after the query runs, so you can practise without breaking anything.
Syntax
INSERT INTO table_name (col1, col2) VALUES (val1, val2), (val3, val4);
Try it yourself
Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.
insert.sql · PostgreSQL
Loading editor…
Examples
1. Insert one row
INSERT INTO departments (id, name, location, budget) VALUES (7, 'Legal', 'Paris', 200000); SELECT * FROM departments;
The new department appears in the result.
Common mistakes
- Leaving out the column list, which breaks when the table changes.
- Inserting a duplicate primary key.
Practice: test yourself
This challenge is from our interactive course. Write the query and press Check answer.
Challenge · 20 pts
Add a new department with id 7, name 'Legal', location 'Paris' and budget 200000.
challenge.sql · PostgreSQL
Loading editor…
FAQ
How do I insert the result of a query?
INSERT INTO target (cols) SELECT ... FROM source;
Learn SQL step by step
65 hands-on exercises, streaks, a leaderboard and a certificate.