01 / 21

SQL INSERT
Statement

Learn how to add new rows to database tables using the INSERT statement: the fundamental command for writing data in SQL.

SQL Database Fundamentals

What Is INSERT?

The INSERT statement is one of SQL's four core data manipulation commands. It lets you add new rows of data to an existing table in your database. Every time a user signs up, a purchase is made, or a score is recorded, an INSERT is happening behind the scenes.

The Four DML Commands

SELECT
Read data from tables
INSERT
Add new rows to tables
UPDATE
Modify existing rows
DELETE
Remove rows from tables

Anatomy of a Table

id name grade email 1 Alice 10 alice@hs.edu 2 Bob 11 bob@hs.edu 3 Carol 10 carol@hs.edu 4 ??? ??? ??? → students table

A table has columns (the schema: id, name, grade, email) and rows (actual data). INSERT adds a new row at the bottom.

The Basic Syntax

INSERT INTO table_name (column1, column2, column3) VALUES ('value1', 'value2', 'value3');
1
INSERT INTO: tells the database you want to add data.
2
table_name: the target table receiving the new row.
3
(columns): which columns you are providing values for.
4
VALUES (...): the actual data, in the same order as the columns.

INSERT With All Columns

-- Explicit column list (recommended) INSERT INTO students (id, name, grade, email) VALUES (4, 'Diana', 12, 'diana@hs.edu');
-- Omitting column list (fragile) INSERT INTO students VALUES (4, 'Diana', 12, 'diana@hs.edu');

Explicit vs Implicit Column Lists

Explicit Columns Implicit (No Columns)
Syntax INSERT INTO t (a, b) VALUES (...) INSERT INTO t VALUES (...)
Order matters? Matches your list Must match table definition
Partial insert? ✓ Yes ✕ No
Resilient to schema changes? ✓ Yes ✕ No
Readability Clear intent Ambiguous

Inserting Partial Data

You don't always have values for every column. Columns you skip will receive their default value (often NULL). This only works with an explicit column list.

-- Only providing name and grade (id auto-increments, email defaults to NULL) INSERT INTO students (name, grade) VALUES ('Eve', 9);

Inserting Multiple Rows

You can insert several rows in a single statement by separating each set of values with a comma. This is faster than running multiple individual INSERT statements.

INSERT INTO students (name, grade, email) VALUES ('Frank', 10, 'frank@hs.edu'), ('Grace', 11, 'grace@hs.edu'), ('Hank', 12, 'hank@hs.edu');

How to Write an INSERT

1
Identify the target table. Which table needs the new data?
2
List the columns. Decide which columns you are supplying values for.
3
Match data types. Strings go in quotes; numbers do not; dates use the correct format.
4
Order values correctly. The first value maps to the first column, and so on.
5
End with a semicolon. It's best practice to terminate every SQL statement with a ;

Example 1: Simple Insert

Add a new book to a books table with columns: title, author, and year.

INSERT INTO books (title, author, year) VALUES ('1984', 'George Orwell', 1949);

Example 2: Multi-Row Insert

Add three new products to an inventory table with columns: product_name, price, quantity.

INSERT INTO inventory (product_name, price, quantity) VALUES ('Notebook', 3.99, 200), ('Pen', 1.49, 500), ('Eraser', 0.79, 300);

Example 3: NULL and DEFAULT

An employees table has: id (auto-increment), name, department, hire_date (defaults to today).

-- Let id auto-generate, let hire_date use its default INSERT INTO employees (name, department) VALUES ('Priya Patel', 'Engineering');
-- Explicitly insert NULL for department INSERT INTO employees (name, department) VALUES ('James Lee', NULL);

Data Types Matter

Strings
Wrap in single quotes: 'hello'
Numbers
No quotes needed: 42, 3.14
Dates
Use quotes with format: '2024-01-15'
NULL
No quotes, just the keyword: NULL

Common Mistakes

✕ Mismatched Count
Number of values doesn't match the number of columns listed.
✕ Wrong Quotes
Using double quotes instead of single quotes for string values.
✕ Wrong Order
Values in a different order than the column list: name and grade get swapped.
✕ Quoting NULL
Writing 'NULL' (a string) instead of NULL (the keyword).
✕ Missing Semicolon
Forgetting the ; at the end causes the next statement to merge or errors.
✕ Violating Constraints
Inserting a duplicate primary key or NULL into a NOT NULL column.

INSERT Cheat Sheet

Pattern Syntax Use When
Single row, all cols INSERT INTO t (a,b) VALUES (1,2); Adding one complete record
Single row, partial INSERT INTO t (a) VALUES (1); Some columns have defaults
Multiple rows INSERT INTO t (a,b) VALUES (1,2),(3,4); Batch loading data
With NULL INSERT INTO t (a,b) VALUES (1, NULL); Value is unknown or optional

Question 1 of 5

Which keyword pair begins every INSERT statement?

Question 2 of 5

What is wrong with this statement?
INSERT INTO books (title, year) VALUES ('Dune', '1965');

Question 3 of 5

You want to insert a row but leave the email column empty. The column allows NULL. Which is correct?

Question 4 of 5

How do you insert three rows in a single INSERT statement?

Question 5 of 5

Why is it best practice to always include the column list in an INSERT statement?