🌱BeginnerData Manipulation
UPDATE
Modify existing rows in a table using the UPDATE statement.
Overview
UPDATE modifies existing rows in a table. Always pair it with a WHERE clause an UPDATE without WHERE changes every single row in the table, which is almost never what you want. This is one of the most common and costly mistakes in SQL.
You can update multiple columns in a single statement, use expressions based on the current column value (e.g., quantity = quantity + 1), and reference values from other tables using a FROM clause (UPDATE ... FROM). The RETURNING clause lets you retrieve the modified rows without a follow-up SELECT.
Key Points
- •Always use WHERE with UPDATE without it, every row is updated
- •Update multiple columns at once: SET col1 = v1, col2 = v2
- •Use RETURNING to see what was changed: UPDATE ... RETURNING *
- •Reference current value: SET views = views + 1
- •UPDATE ... FROM joins to another table for conditional updates
Examples
Basic UPDATE with WHERE
-- Update a single row
UPDATE users SET email = 'new@example.com' WHERE id = 42;
-- Update multiple columns
UPDATE products
SET price = 29.99, updated_at = NOW()
WHERE id = 7;
-- Update based on current value
UPDATE products SET stock = stock - 1 WHERE id = 7;UPDATE with RETURNING
UPDATE orders
SET status = 'shipped', shipped_at = NOW()
WHERE id = 1001
RETURNING id, status, shipped_at;Common Mistakes
- Omitting the WHERE clause runs but updates all rows silently. Always double-check with a SELECT first.
Exercises
Write SQL to solve each exercise. Your progress is saved automatically.
Loading editor…
Completed: 0 / 5 exercises
Knowledge Check
Question 1 of 4