Skip to content
Lesson 8 of 10 · SQL for Beginners

Changing data

Add, change and remove rows with INSERT, UPDATE and DELETE, and avoid the forgotten WHERE.

18 min

Until now you only read. Three statements change data.

sql2 lines
INSERT INTO books (id, title, author, genre, price, year, stock)
VALUES (11, 'Tide Tables', 'R. Okafor', 'nonfiction', 15.00, 2024, 10);

Name the columns, then give the values in the same order. Columns you leave out get their default, usually NULL. Add several rows with VALUES (...), (...), (...).

sql3 lines
UPDATE books
SET price = ROUND(price * 1.1, 2)
WHERE genre = 'cooking';

SET lists the new values; WHERE says which rows to change.

sql1 line
DELETE FROM books WHERE stock = 0;

UPDATE and DELETE without WHERE change every row. There is no confirmation and no undo. Habits that protect you:

  1. 1.Write the WHERE first, or write it as a SELECT first (SELECT * FROM books WHERE stock = 0;), check the rows, then change SELECT * into DELETE.
  2. 2.Use a primary key to target one row: WHERE id = 5.
  3. 3.On a real database, wrap changes in a transaction: BEGIN; ... then COMMIT; to keep them or ROLLBACK; to undo.

In the exercises below you work on a private copy of the data each time, so you can experiment freely. In your own database, take a backup before you try anything risky.

Decide

You run the bookshop database. Rivers of Salt (id 5) is out of print, so you must delete it. You type the statement quickly before a meeting.

What do you type?

Exercise1 check
sql

Add a new book: id 11, title 'Tide Tables', author 'R. Okafor', genre 'nonfiction', price 15.00, year 2024, stock 10. Then show its title and price.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
starter code4 lines
INSERT INTO books (id, title, author, genre, price, year, stock)
VALUES ;

SELECT title, price FROM books WHERE id = 11;
Exercise1 check
sql

Raise the price of every cooking book by 10 percent, rounded to 2 decimals. Then show the title and price of the cooking books.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
starter code5 lines
UPDATE books
SET price = 
WHERE ;

SELECT title, price FROM books WHERE genre = 'cooking';
Exercise1 check
sql

Delete every book with no stock (stock equal to 0). Then show how many books are left, as left_over.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
starter code3 lines
DELETE FROM books WHERE ;

SELECT COUNT(*) AS left_over FROM books;
Checklist
0/5

Before you run UPDATE or DELETE

  • INSERT INTO t (cols) VALUES (...) adds rows; UPDATE t SET ... WHERE ... changes them; DELETE FROM t WHERE ... removes them.
  • Without WHERE, UPDATE and DELETE affect every row.
  • Preview with a SELECT using the same WHERE before you change anything.
  • Transactions (BEGIN, COMMIT, ROLLBACK) and backups make mistakes recoverable.
Check yourself

Check your safety habits.

1Multiple choice

What does UPDATE books SET stock = 0; do?

  • ASets stock to 0 for every book
  • BDoes nothing without WHERE
  • CSets it for the first row only
  • DRaises an error
2Multiple choice

Which command undoes the changes since BEGIN?

  • ACOMMIT
  • BROLLBACK
  • CUNDO
  • DRESET
3Multiple choice

Which is the safest way to remove one specific book?

  • A`DELETE FROM books;`
  • B`DELETE FROM books WHERE id = 5;`
  • C`DROP TABLE books;`
  • D`UPDATE books SET stock = NULL;`
4Put in order

Put the safe workflow in order.

  • ACheck the rows it returns
  • BRun the DELETE with the same WHERE
  • CWrite a SELECT with the WHERE condition
  • DStart a transaction with BEGIN
  • ECOMMIT once the result looks right
5Fill the blanks

In an INSERT, the values follow the keyword .

Capitalisation, spacing and punctuation are ignored when grading.

Practise this lesson, not just read it

In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.