>_ IzzyLab

Hands-on task

This task is step 6 of the course “SQL with SQLite”

SQLite: update rows by condition (UPDATE)

easy~3 min

The database /root/shop.db has a table products(id, name, price). For products with a price of 1000 and above, lower the price by 100 (that is, decrease their price by 100). The prices of the other products must not be touched.

The changes must be saved in the database itself (this is an UPDATE, not a selection into a file).

What you'll practise:
  • sqlite3
  • update
  • where
  • data modification
  • set

Topic breakdown

UPDATE changes existing rows in place, right in the database — it's a data modification, not a selection. The SET clause assigns new column values, and the new value can be expressed via the old one (the column appears both on the left and the right). It's critical to remember: an UPDATE without WHERE will affect all rows in the table, so the range of modified records is always limited with a condition.

What's checked

  1. price of product B lowered by 100
  2. price of product C lowered by 100
  3. price of cheap product A unchanged

What it looks like