Lesson 18 of 30 · Changing Data
SQL INSERT INTO: Add Rows to a Table
So far you've only read data. Real applications also write it: a player signs up, an order is placed, a message is sent. Each of those is an INSERT, the SQL statement that adds new rows to a table.
INSERT INTO … VALUES
INSERT INTO guilds (name, city, founded)
VALUES ('Storm Riders', 'Saltcliff', 2025);
INSERT INTO guildsis the table that gets the new row.(name, city, founded)lists the columns you're filling, in any order you like.VALUES (…)gives one value per column, in the same order as the list. Text and dates go in single quotes; numbers don't.
Press Run and the editor reports 1 row changed. Add SELECT * FROM guilds; after the insert to see the new row.
Auto ids: INTEGER PRIMARY KEY
We didn't give the guild an id. The guilds table declares id INTEGER PRIMARY KEY, and in SQLite such a column numbers itself: leave it out and the new row gets the largest existing id plus one. Guilds 1 to 5 exist, so Storm Riders becomes guild 6. Letting the database pick ids is the normal way to work, because you can never reuse one by accident. SELECT last_insert_rowid(); shows the id it just picked.
Several rows at once
One INSERT can add many rows: write one group in parentheses per row, separated by commas.
INSERT INTO quests (title, region, difficulty, reward) VALUES
('Spider Nest', 'Greywood', 'medium', 190),
('Lantern Festival', 'Eldermoor', 'easy', 30);
All rows share the same column list, so every group needs the same number of values.
Defaults and NULL
Columns you leave out still get a value:
- the column's default, if it has one.
purchases.quantityis declaredINTEGER NOT NULL DEFAULT 1, so a purchase without a quantity counts as one item; - otherwise
NULL("no value"), which is fine forheroes.guild_id: heroes without a guild haveNULLthere; - but a
NOT NULLcolumn without a default must be given, or the insert fails.
INSERT INTO purchases (hero_id, item_id, bought_on)
VALUES (9, 9, '2025-06-01'); -- quantity becomes 1
The tables you'll write to:
| Table | Columns |
|---|---|
heroes |
id, name, class, level, gold, guild_id (may be NULL), joined (date as text, 'YYYY-MM-DD') |
quests |
id, title, region, difficulty ('easy', 'medium' or 'hard'), reward |
purchases |
id, hero_id, item_id, quantity (default 1), bought_on |
Common mistakes
- Value count doesn't match the column list.
INSERT INTO guilds (name, city) VALUES ('A', 'B', 2025);fails with3 values for 2 columns. - Leaving out a required column. Without
joinedyou getNOT NULL constraint failed: heroes.joined. - Giving an id that's taken. Hero 3 already exists, so
id3 fails withUNIQUE constraint failed: heroes.id. Leave the id out. - Skipping the column list.
INSERT INTO heroes VALUES (…)only works if you give every column in the table's exact order, and it breaks as soon as someone adds a column. Always name the columns. - An apostrophe inside text. Double it:
'Mira''s Lantern'.
Assignment
Welcome a new hero and stock the quest board with three INSERT statements:
- Add the hero Pip Thistledown: class
'Rogue', level1,50gold, joined'2025-08-01'. Pip has no guild, so leaveguild_idout. Leaveidout too: SQLite will make Pip hero 16. - Add these two quests with one
INSERT:'The Sunken Bell'in'Saltcliff', difficulty'medium', reward240'Goblin Toll Bridge'in'Greywood', difficulty'easy', reward70
- Record Pip's first purchase: item
12(a Scroll of Return) bought on'2025-08-02'. Leavequantityout so the default applies.
Premium lesson
Unlock the full Learn SQL course
- All 30 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.
Lesson last updated on .