Before you start, you need
Select data with SELECT and FROM · Work correctly with NULL values
Add rows by matching columns and values
A query with SELECT reads what already exists. INSERT adds a new row. For example, a new product needs its id, name, category, price and stock.
After INSERT INTO products, write the list of columns in parentheses. VALUES introduces another list with the values. Each value goes into the column in the same position: if you swap price and stock, you will store the wrong data.
Text values take single quotes, and the numbers in these examples do not. You can add several rows with comma-separated lists of values, using the same column list.
Then query the row by its id to check it. In the lab, each attempt starts with the initial data: include the INSERT and the SELECT in the same attempt. A column you leave out gets its default value, or NULL if the table allows it. If you leave out an id declared as INTEGER PRIMARY KEY, SQLite assigns the next free number.
INSERT INTO table_name (column_1, column_2)
VALUES (value_1, value_2);See it in action
We want to add Case to the catalogue and check its stock.
INSERT INTO products (id, name, category, price, stock)
VALUES (20, 'Case', 'Accessories', 10, 3);
SELECT id, name, stock
FROM products
WHERE id = 20;| id | name | stock |
|---|---|---|
| 20 | Case | 3 |
- The list declares id, name, category, price and stock. VALUES supplies a value for each position.
- The SELECT afterwards finds the new product with id 20 and stock 3. The existing rows remain.
- If you swap 10 and 3 without changing the columns, price and stock will no longer match the requirement.
- In the lab, each attempt goes back to the starting data. Include the insert and the check in the same attempt if you want to see the change.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Add the product Stand
Write the SQL statement that adds the product with id 6, name Stand, category Accessories, price 18 and stock 20.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Match each attribute of the new product with the value in the same position.
Keep the same order in VALUES.
Numbers do not need quotes.
Solution explained
INSERT INTO products (id, name, category, price, stock) VALUES (6, 'Stand', 'Accessories', 18, 20);The statement makes it clear which value goes into each column.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Add two rows and check them
Insert into products the rows (20, 'Case', 'Accessories', 10, 3) and (21, 'Base', 'Accessories', 15, 4) for id, name, category, price and stock. Then query id, name and stock for those two ids, sorted by id.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Match each column with its value before you write.
An INSERT statement can contain two VALUES lists.
The checking SELECT must be in the same attempt.
Solution explained
INSERT INTO products (id, name, category, price, stock) VALUES (20, 'Case', 'Accessories', 10, 3), (21, 'Base', 'Accessories', 15, 4); SELECT id, name, stock FROM products WHERE id IN (20, 21) ORDER BY id;Both rows are added in one statement, and the SELECT lets you check their values.
Key takeaways
- INSERT adds rows.
- Columns and values are matched by position.
- Declare the columns to make the intent visible.
- Query the inserted row to check it.
Next: Update rows with UPDATE