UPDATE SET WHERE
The UPDATE statement modifies existing data in a table. You specify which table to update, which columns to change using SET, and which rows to affect using WHERE.
Always use a WHERE clause with UPDATE. Without it, the update will apply to every single row in the table, which is almost never what you want.
- UPDATE: Specifies the table to modify
- SET: Defines the new values for one or more columns
- WHERE: Filters which rows to update — always include this!
Example
-- Update a single column for one user
UPDATE users
SET age = 29
WHERE username = 'alice';
-- Update multiple columns at once
UPDATE users
SET email = 'newalice@example.com', bio = 'Senior developer'
WHERE username = 'alice';
-- Verify the update
SELECT * FROM users WHERE username = 'alice'; Notes
- CRITICAL: Always run a SELECT with the same WHERE clause first to verify which rows will be affected before running an UPDATE.
Conditional Updates & Updating Multiple Rows
You can update multiple rows at once by using a WHERE clause that matches more than one record. You can also use expressions and calculations in the SET clause.
The CASE expression lets you apply different updates to different rows within a single UPDATE statement, which is very powerful and efficient.
Example
-- Update all inactive users
UPDATE users
SET is_active = FALSE
WHERE join_date < '2025-01-01';
-- Increment age for all users (e.g., a year has passed)
UPDATE users
SET age = age + 1;
-- Conditional update with CASE
UPDATE users
SET bio = CASE
WHEN age < 25 THEN 'Young professional'
WHEN age BETWEEN 25 AND 35 THEN 'Mid-career'
ELSE 'Experienced professional'
END
WHERE bio IS NULL;
-- Verify changes
SELECT username, age, bio FROM users; Notes
- The CASE expression in SQL works like an if-else chain. It is extremely useful in both UPDATE and SELECT statements.
