Lesson 9 of 17
Updating Data
UPDATE
The UPDATE statement modifies existing rows in a table:
UPDATE products SET price = 89.99 WHERE name = 'Headphones';
Always include a WHERE clause unless you intentionally want to update every row. Without WHERE, UPDATE modifies all rows in the table.
Updating Multiple Columns
Separate multiple column assignments with commas:
UPDATE products
SET price = 89.99, category = 'Audio'
WHERE name = 'Headphones';
Using Expressions
You can use expressions in SET:
-- Apply a 10% discount to all Electronics
UPDATE products
SET price = price * 0.9
WHERE category = 'Electronics';
-- Increase all Office product prices by $5
UPDATE products
SET price = price + 5
WHERE category = 'Office';
Updating by Primary Key
The most precise and safe way to update a single row:
UPDATE users SET email = 'newalice@example.com' WHERE id = 1;
UPDATE with Subquery
You can use a subquery in the WHERE clause:
-- Update orders for a specific customer by name
UPDATE orders
SET quantity = 3
WHERE customer_id = (SELECT id FROM customers WHERE name = 'Alice');
Checking How Many Rows Were Updated
SQLite does not return affected row counts directly in SQL. In most drivers, you can call changes():
UPDATE products SET price = 99.99 WHERE category = 'Office';
SELECT changes(); -- number of rows affected by the last statement
Your Task
Update the price of the 'Coffee Maker' product to 54.99. Then select all Kitchen products to verify.
SQLite runtime loading...
Loading...
Click "Run" to execute your code.