SQL - UPDATE
Overview
Always include a WHERE when updating unless you intend to change all rows.
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 1;
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 1;
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 1;
Prerequisite
Load the Standard Test Data.
Expected Output (live; wrapped in a transaction)
id | first_name | department_id | salary |
---|---|---|---|
1 | Ada | 1 | 126000 |
2 | Bob | 1 | 94500 |
3 | Chen | 2 | 80000 |
4 | Dee | 3 | 110000 |
5 | Eli | 1 | 136500 |
Sample Use Cases (4)
1) Give 10% raise to all Engineers (dept_id=1)
id | first_name | salary |
---|---|---|
1 | Ada | 132000 |
2 | Bob | 99000 |
3 | Chen | 80000 |
4 | Dee | 110000 |
5 | Eli | 143000 |
2) Set city='Remote' where city IS NULL
id | first_name | city |
---|---|---|
1 | Ada | San Francisco |
2 | Bob | New York |
3 | Chen | Seattle |
4 | Dee | Austin |
5 | Eli | Boston |
3) Standardize title casing
id | first_name | title |
---|---|---|
1 | Ada | Engineer |
2 | Bob | Engineer |
3 | Chen | HR Specialist |
4 | Dee | Sales Manager |
5 | Eli | Principal Engineer |
4) Set hired_at to current time if NULL
id | first_name | hired_at |
---|---|---|
1 | Ada | 2021-01-10 10:00:00 |
2 | Bob | 2021-05-05 09:30:00 |
3 | Chen | 2025-09-17 20:53:46 |
4 | Dee | 2022-03-01 12:00:00 |
5 | Eli | 2020-11-20 15:15:00 |