Lessons › SQL › Chapter 7: Creating and changing tables
SQL lesson
UPDATE SET
UPDATE changes values that are already in a table. SET says which column gets the new value. WHERE says which rows to change. Always write the WHERE. Without it, every row in the table changes.
Try this lesson free →Example
Change values that are already there
UPDATE orders
SET revenue = 95.00
WHERE order_id = 'O2003'| order_id | category | revenue |
|---|---|---|
| O2001 | Electronics | 299.99 |
| O2002 | Electronics | 149.50 |
| O2003 | Clothing | 95.00 |
| O2004 | Electronics | 149.50 |
Step by step
- The table you're changing.
UPDATE orders SET revenue = 95.00 WHERE order_id = 'O2003' - Which column, and what it becomes.
UPDATE orders SET revenue = 95.00 WHERE order_id = 'O2003' - Only these rows change. Leave it out and every row changes.
UPDATE orders SET revenue = 95.00 WHERE order_id = 'O2003'
Practice
1. Order O2003 should be 95.00, not 89.00. Fix its revenue in orders.
UPDATE orders SET revenue = 95.00 WHERE order_id = 'O2003'2. The Lagos store's target for 2024-04-01 should be 6000. Change it in targets.
UPDATE targets SET target = 6000 WHERE store = 'Lagos' AND month = '2024-04-01'3. The Accra store has sold out of P2002. Set its units to 0 in stock_counts.
UPDATE stock_counts SET units = 0 WHERE product_id = 'P2002' AND store = 'Accra'4. Order O2004 is in the wrong category. Change its category to Clothing in orders.
UPDATE orders SET category = 'Clothing' WHERE order_id = 'O2004'5. Every target for the Lagos store goes up to 6000. Change them in targets.
UPDATE targets SET target = 6000 WHERE store = 'Lagos'Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →