SQL INSERT INTO Statement
beginnerbasics
2 min read
What is INSERT INTO?
The INSERT INTO statement adds new rows to a table. It is one of the four core DML (Data Manipulation Language) commands alongside SELECT, UPDATE, and DELETE.
Syntax
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);Multi-row insert:
INSERT INTO table_name (column1, column2)
VALUES
(val1a, val2a),
(val1b, val2b);When to Use
- Adding a new user, order, or product to the database
- Seeding tables with initial data
- Copying rows from one table to another with
INSERT INTO ... SELECT
Key Points
- Column List — Always specify the column list for clarity, even if inserting into every column.
- Default Values — Omitted columns receive their default value (often NULL or a sequence value).
- Multi-row Insert — PostgreSQL supports inserting multiple rows in a single statement, which is much faster than individual inserts.
- RETURNING — PostgreSQL lets you append
RETURNING *to get the inserted rows back immediately. - Constraints — If a value violates a NOT NULL, UNIQUE, or CHECK constraint, the insert fails with an error.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL INSERT INTO Statement Challenge
Write a query that solve this task: add one new student to the students table.
Expected result
One row inserted. The students table now contains the new record.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is INSERT INTO?
Initializing database...Each run starts from fresh sample data.
More Examples
Insert multiple products
Add three products in a single statement.
Initializing database...Each run starts from fresh sample data.
Insert with RETURNING
Insert a row and immediately get the generated ID back.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Do I have to list all columns in INSERT INTO?
No. You only need to list the columns you are providing values for. Omitted columns use their default value or NULL.
How do I insert data from another table?
Use INSERT INTO ... SELECT: INSERT INTO archive_students (first_name, email) SELECT first_name, email FROM students WHERE graduated = true;