Module 6 · Changing data

Add rows with INSERT

By the end you will be able to add rows and check that their values are in the correct columns.

About 12 min

0 of 52 lessons

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.

Minimal syntax
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.

SQL
INSERT INTO products (id, name, category, price, stock)
VALUES (20, 'Case', 'Accessories', 10, 3);

SELECT id, name, stock
FROM products
WHERE id = 20;
Example result: 1 rows
idnamestock
20Case3
  1. The list declares id, name, category, price and stock. VALUES supplies a value for each position.
  2. The SELECT afterwards finds the new product with id 20 and stock 3. The existing rows remain.
  3. If you swap 10 and 3 without changing the columns, price and stock will no longer match the requirement.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Add two rows and check them

Not started

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.

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