SQL INSERT INTO Get Certified
Learn how to add new records to database tables using the SQL INSERT INTO statement.
What is the INSERT INTO Statement?
The INSERT INTO statement is used to add new records to a database table. You can insert a single row, multiple rows, or data selected from another table.
Basic INSERT INTO Syntax
The basic syntax specifies the table name, the columns receiving data, and the values that should be inserted.
What this code does: Creates an empty planets table with three columns, then uses INSERT INTO to add one row. The values are matched to the column list by position, so 1 goes into id, ‘Mars’ into name and ‘Terrestrial’ into type. The final SELECT displays the table so you can see the new Mars record.
CREATE TABLE planets (
id INTEGER,
name TEXT,
type TEXT
);
INSERT INTO planets (id, name, type)
VALUES (1, 'Mars', 'Terrestrial');
SELECT * FROM planets;
Inserting a Single Record
You can add one complete record by providing values for the required columns.
What this code does: Builds a museums table and adds a single complete record for the Science Museum in Delhi. Every column in the table receives a value, which is the most common way to add one new row. Running the SELECT afterwards shows a table with exactly one row.
CREATE TABLE museums (
id INTEGER,
name TEXT,
city TEXT
);
INSERT INTO museums (id, name, city)
VALUES (1, 'Science Museum', 'Delhi');
SELECT * FROM museums;
Inserting Multiple Records
Multiple records can be inserted with one INSERT INTO statement by separating each row with a comma.
What this code does: Adds four musical instruments to the instruments table using one INSERT INTO statement. Each row sits inside its own parentheses and the rows are separated by commas, which is faster and tidier than writing four separate statements. The final query returns all four rows: Piano, Violin, Flute and Drums.
CREATE TABLE instruments (
id INTEGER,
name TEXT,
family TEXT
);
INSERT INTO instruments (id, name, family)
VALUES
(1, 'Piano', 'Keyboard'),
(2, 'Violin', 'Strings'),
(3, 'Flute', 'Woodwind'),
(4, 'Drums', 'Percussion');
SELECT * FROM instruments;
Inserting Data into Selected Columns
You can specify only the columns that need values. Columns that are not specified can receive their default value or NULL.
What this code does: Creates an apartments table with four columns, but the INSERT INTO lists only id, owner and city. Because floor is left out and has no default, SQLite stores NULL in it. The result shows Michael’s apartment in Mumbai with an empty (NULL) floor value.
CREATE TABLE apartments (
id INTEGER,
owner TEXT,
city TEXT,
floor INTEGER
);
INSERT INTO apartments (id, owner, city)
VALUES (101, 'Michael', 'Mumbai');
SELECT * FROM apartments;
INSERT with NULL Values
If a value is unknown or unavailable, NULL can be inserted into a column that allows NULL values.
What this code does: Records a delivery for Emily that has not arrived yet. The delivered_date is not known, so the statement writes NULL explicitly instead of inventing a date. The SELECT then shows the row with an empty delivery date, which could be filled in later with an UPDATE.
CREATE TABLE deliveries (
id INTEGER,
customer TEXT,
address TEXT,
delivered_date TEXT
);
INSERT INTO deliveries (id, customer, address, delivered_date)
VALUES (501, 'Emily', 'Sector 12', NULL);
SELECT * FROM deliveries;
INSERT with Default Values
A column can have a default value. If that column is omitted from the INSERT statement, SQLite uses the defined default value.
What this code does: Defines a status column with DEFAULT 'Open', then inserts a support ticket without mentioning that column. SQLite fills in ‘Open’ automatically, so the new ticket 1001 appears with the status Open even though the insert never supplied it. Default values keep new records consistent without extra typing.
CREATE TABLE tickets (
id INTEGER,
subject TEXT,
status TEXT DEFAULT 'Open'
);
INSERT INTO tickets (id, subject)
VALUES (1001, 'Website login issue');
SELECT * FROM tickets;
INSERT with an Automatically Generated ID
SQLite can automatically generate an integer primary key. This means you do not need to provide the ID manually.
What this code does: Declares id as INTEGER PRIMARY KEY, which makes SQLite assign the next number automatically. Two recipes are inserted without an id, and they receive ids 1 and 2 in the order they were added. This is the usual way to give every row a unique identifier without managing the numbers yourself.
CREATE TABLE recipes (
id INTEGER PRIMARY KEY,
name TEXT,
difficulty TEXT
);
INSERT INTO recipes (name, difficulty)
VALUES ('Vegetable Pasta', 'Easy');
INSERT INTO recipes (name, difficulty)
VALUES ('Mushroom Soup', 'Medium');
SELECT * FROM recipes;
INSERT Text and Numeric Values
SQL allows different types of values to be inserted into their corresponding columns.
What this code does: Inserts one bicycle using three kinds of values: the text ‘Trail Rider’ (in single quotes), the whole number 18 for gears, and the decimal number 12999.75 for the price. Text values need quotes, while numbers are written without them. The SELECT confirms each value was stored in the right column.
CREATE TABLE bicycles (
id INTEGER,
model TEXT,
gears INTEGER,
price REAL
);
INSERT INTO bicycles (id, model, gears, price)
VALUES (7, 'Trail Rider', 18, 12999.75);
SELECT * FROM bicycles;
INSERT Data from Another Table
The INSERT INTO ... SELECT syntax can copy matching rows from one table into another table.
What this code does: Fills a courses table with three courses, creates an empty long_courses table, and then copies into it only the courses that last more than 10 hours. Physics (12) and Mathematics (15) are copied, while Chemistry (8) is skipped. The final query shows just those two rows in long_courses.
CREATE TABLE courses (
id INTEGER,
name TEXT,
duration INTEGER
);
INSERT INTO courses (id, name, duration)
VALUES
(1, 'Physics', 12),
(2, 'Chemistry', 8),
(3, 'Mathematics', 15);
CREATE TABLE long_courses (
id INTEGER,
name TEXT,
duration INTEGER
);
INSERT INTO long_courses (id, name, duration)
SELECT id, name, duration
FROM courses
WHERE duration > 10;
SELECT * FROM long_courses;
INSERT OR IGNORE
SQLite’s INSERT OR IGNORE can skip an insert when the new record would violate a UNIQUE or PRIMARY KEY constraint.
What this code does: The code column is marked UNIQUE, so no two coupons can share the same code. The first insert adds SAVE10 normally. The second insert tries to add SAVE10 again, but OR IGNORE makes SQLite quietly skip it instead of raising an error. The table ends up with only one row.
CREATE TABLE coupons (
id INTEGER PRIMARY KEY,
code TEXT UNIQUE
);
INSERT INTO coupons (id, code)
VALUES (1, 'SAVE10');
INSERT OR IGNORE INTO coupons (id, code)
VALUES (2, 'SAVE10');
SELECT * FROM coupons;
INSERT OR REPLACE
SQLite’s INSERT OR REPLACE can replace an existing row when a PRIMARY KEY or UNIQUE constraint conflicts.
What this code does: Inserts a profile with id 1 and level 5. The second statement uses the same id, so instead of failing, OR REPLACE deletes the old row and puts in the new one with level 6. The result has a single row for ‘nova’ at level 6, which makes this a handy way to update-or-insert a record in one step.
CREATE TABLE profiles (
id INTEGER PRIMARY KEY,
username TEXT,
level INTEGER
);
INSERT INTO profiles (id, username, level)
VALUES (1, 'nova', 5);
INSERT OR REPLACE INTO profiles (id, username, level)
VALUES (1, 'nova', 6);
SELECT * FROM profiles;
INSERT Using SELECT with a Calculated Value
A SELECT query can calculate values before inserting them into another table.
What this code does: Stores three product sales, then builds a sales_summary table by inserting the product name together with quantity * price. The multiplication happens during the SELECT, so the summary holds ready-made totals: Desk Lamp 3400, Wall Clock 2400 and Study Chair 13500.
CREATE TABLE sales (
product TEXT,
quantity INTEGER,
price REAL
);
INSERT INTO sales (product, quantity, price)
VALUES
('Desk Lamp', 4, 850.00),
('Wall Clock', 2, 1200.00),
('Study Chair', 3, 4500.00);
CREATE TABLE sales_summary (
product TEXT,
total_value REAL
);
INSERT INTO sales_summary (product, total_value)
SELECT product, quantity * price
FROM sales;
SELECT * FROM sales_summary;
INSERT with a Condition
When using INSERT INTO ... SELECT, a WHERE clause can control which records are inserted.
What this code does: Starts with four applicants and copies only those who scored 80 or higher into a selected_applicants table. James (91) and Sophia (84) pass the WHERE filter and are inserted. Olivia (72) and Ethan (65) are left behind, so the final query returns just the two selected applicants.
CREATE TABLE applicants (
id INTEGER,
name TEXT,
score INTEGER
);
INSERT INTO applicants (id, name, score)
VALUES
(1, 'Olivia', 72),
(2, 'James', 91),
(3, 'Sophia', 84),
(4, 'Ethan', 65);
CREATE TABLE selected_applicants (
id INTEGER,
name TEXT,
score INTEGER
);
INSERT INTO selected_applicants (id, name, score)
SELECT id, name, score
FROM applicants
WHERE score >= 80;
SELECT * FROM selected_applicants;
INSERT INTO Comparison
VALUES (1, 'Mars')VALUES (...), (...)INSERT INTO archive SELECT ...INSERT OR IGNORE INTO ...INSERT OR REPLACE INTO ...Advantages of INSERT INTO
- Adds new records to database tables.
- Supports single and multiple row insertion.
- Allows specific columns to be populated.
- Can copy data from one table to another.
- Works with default and
NULLvalues. - SQLite provides additional conflict-handling options.
Best Practices
- Specify column names explicitly whenever possible.
- Make sure the number and order of values match the selected columns.
- Use appropriate data types for inserted values.
- Use transactions when inserting many related records.
- Check
UNIQUEandPRIMARY KEYconstraints before inserting data. - Be careful when using
INSERT OR REPLACEbecause it can replace an existing row.
The INSERT INTO statement is one of the fundamental SQL commands for adding data. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.