UPDATE Statement
UPDATE modifies existing rows in a table. Always pair it with a WHERE clause to target specific rows — without WHERE, every row in the table gets changed. You can update one or multiple columns at once.
UPDATE Syntax & Rules
- UPDATE table SET col = val WHERE condition
- Comma-separate multiple column updates: SET col1 = v1, col2 = v2
- Use expressions: SET price = price * 1.1 (increase 10%)
- SET col = NULL to clear a value
- Without WHERE — ALL rows are updated (dangerous!)
- Use RETURNING (PostgreSQL) to see updated rows
UPDATE Examples
-- Update a single product's price
UPDATE products SET price = 899.99 WHERE id = 1;
-- Update multiple columns at once
UPDATE products
SET price = 549.99, stock = 100
WHERE name = 'Phone';
-- Update using an expression (10% price increase for all)
UPDATE products SET price = price * 1.10;
-- WARNING: no WHERE — updates ALL products!
-- Update order status
UPDATE orders SET status = 'shipped' WHERE id = 2;
-- Update only if condition met
UPDATE products SET stock = 0 WHERE stock < 5;
-- PostgreSQL: RETURNING shows what changed
UPDATE orders
SET status = 'delivered'
WHERE id = 1
RETURNING id, status, total_amount;
-- Conditional update with CASE
UPDATE products
SET price = CASE
WHEN price > 500 THEN price * 0.95 -- 5% off expensive items
ELSE price * 0.90 -- 10% off cheaper items
END;Quick Quiz
Tip
Tip
Practice UPDATE Statement in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Relationships define how tables connect through foreign keys
Common Mistake
Warning
A common mistake with UPDATE Statement is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready sql code.
Practice Task
Note
Practice Task — (1) Write a working example of UPDATE Statement from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Key Takeaways
- UPDATE modifies existing rows in a table.
- UPDATE table SET col = val WHERE condition
- Comma-separate multiple column updates: SET col1 = v1, col2 = v2
- Use expressions: SET price = price * 1.1 (increase 10%)