UPDATE — Changing Existing Data
Modify rows that already exist — and learn why the WHERE clause is the most important part of every UPDATE statement.
UPDATE changes data in existing rows. Without a WHERE clause, it changes EVERY row — always double-check your condition.
EXERCISE
1UPDATE — Changing Existing Data Safely
UPDATE modifies rows that already exist in a table. The WHERE clause is not optional — without it, you change every single row.
Basic UPDATE syntax:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Single column update:
-- Give Ada a raise:
UPDATE employees
SET salary = 82000
WHERE id = 1;
Multiple columns at once:
-- Update department and salary together:
UPDATE employees
SET department_id = 2,
salary = 65000,
updated_at = NOW()
WHERE id = 5;
The WHERE clause is CRITICAL:
-- ❌ THE MOST DANGEROUS SQL YOU CAN WRITE:
UPDATE employees SET salary = 0;
-- This sets EVERY employee's salary to 0. No warning. No undo (unless in a transaction).
-- ✅ Always include WHERE:
UPDATE employees SET salary = 0 WHERE id = 99;
-- Only affects the one row you intend
Safe UPDATE workflow — SELECT first:
-- Step 1: Verify which rows will be affected:
SELECT id, name, salary FROM employees WHERE department_id = 3;
-- Review the output carefully
-- Step 2: Run the UPDATE with the same WHERE:
UPDATE employees SET salary = salary * 1.10 WHERE department_id = 3;
-- 10% raise for department 3 only
UPDATE with expressions:
-- Increase all IT salaries by 15%:
UPDATE employees
SET salary = salary * 1.15
WHERE department_id = (SELECT id FROM departments WHERE name = 'IT');
-- Add 30 days to all pending order due dates:
UPDATE orders
SET due_date = DATE_ADD(due_date, INTERVAL 30 DAY)
WHERE status = 'pending';
Pro Tip: Professional developers always run a
SELECTwith the sameWHEREclause before running anUPDATE. WhatSELECTreturns is exactly whatUPDATEwill modify.
EXERCISE
2UPDATE with JOIN — Updating Across Tables
MySQL lets you UPDATE rows based on conditions from a joined table — combining the power of JOINs with data modification.
Sometimes the condition for which rows to update lives in a different table. MySQL''s UPDATE JOIN syntax handles this elegantly.
UPDATE JOIN syntax:
UPDATE table1 t1
JOIN table2 t2 ON t1.foreign_key = t2.id
SET t1.column = value
WHERE t2.some_column = 'condition';
Example — give all IT department employees a bonus:
UPDATE employees e
JOIN departments d ON e.department_id = d.id
SET e.salary = e.salary * 1.10
WHERE d.name = 'IT';
-- No subquery needed — the JOIN handles the table relationship
Example — mark orders as shipped based on product category:
UPDATE orders o
JOIN products p ON o.product_id = p.id
SET o.status = 'shipped'
WHERE p.category = 'Electronics'
AND o.status = 'processing';
Update one table using values from another:
-- Copy the latest price from products into order_items:
UPDATE order_items oi
JOIN products p ON oi.product_id = p.id
SET oi.unit_price = p.price
WHERE oi.created_at >= '2024-01-01';
Checking rows affected:
-- After any UPDATE, MySQL reports:
-- "X row(s) affected"
-- Use ROW_COUNT() to check programmatically:
UPDATE employees SET status = 'active' WHERE department_id = 1;
SELECT ROW_COUNT() AS rows_updated;
Pro Tip: UPDATE JOIN is often cleaner and faster than using a subquery inside WHERE. It is especially useful when the update condition depends on data from a related table.
EXERCISE
3Common UPDATE Patterns and Pitfalls
These are the UPDATE patterns you will use most often on the job — and the mistakes that cause the most damage when things go wrong.
Pattern 1 — Conditional update with CASE:
-- Set salary tier based on current salary:
UPDATE employees
SET tier = CASE
WHEN salary >= 80000 THEN 'Senior'
WHEN salary >= 60000 THEN 'Mid'
ELSE 'Junior'
END;
Pattern 2 — Update based on aggregate from same table:
-- Flag employees earning below the company average:
UPDATE employees
SET below_average = TRUE
WHERE salary < (SELECT AVG(salary) FROM employees e2);
-- MySQL requires the subquery alias trick (e2) to avoid self-reference error
Pattern 3 — Soft delete (mark as inactive instead of deleting):
-- Never truly delete — just hide:
UPDATE employees
SET is_active = FALSE, deleted_at = NOW()
WHERE id = 42;
-- Then filter in all SELECT queries:
SELECT * FROM employees WHERE is_active = TRUE;
Pattern 4 — Swap two column values:
-- Swap first_name and last_name (interview question!):
UPDATE employees
SET first_name = last_name,
last_name = first_name;
-- MySQL evaluates all SET values from the original row, then applies them
-- So this correctly swaps without a temp variable
The LIMIT safety net:
-- Add LIMIT to cap how many rows can be affected in one UPDATE:
UPDATE employees
SET status = 'reviewed'
WHERE status = 'pending'
LIMIT 100;
-- Never accidentally update more than 100 rows in one shot
Pro Tip: In production, wrap risky UPDATEs in a transaction so you can ROLLBACK if something goes wrong.
START TRANSACTION; UPDATE ...; SELECT * to verify; COMMIT;— always.
