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)

idfirst_namedepartment_idsalary
1Ada1126000
2Bob194500
3Chen280000
4Dee3110000
5Eli1136500

Sample Use Cases (4)

1) Give 10% raise to all Engineers (dept_id=1)

idfirst_namesalary
1Ada132000
2Bob99000
3Chen80000
4Dee110000
5Eli143000

2) Set city='Remote' where city IS NULL

idfirst_namecity
1AdaSan Francisco
2BobNew York
3ChenSeattle
4DeeAustin
5EliBoston

3) Standardize title casing

idfirst_nametitle
1AdaEngineer
2BobEngineer
3ChenHR Specialist
4DeeSales Manager
5EliPrincipal Engineer

4) Set hired_at to current time if NULL

idfirst_namehired_at
1Ada2021-01-10 10:00:00
2Bob2021-05-05 09:30:00
3Chen2025-09-17 20:53:46
4Dee2022-03-01 12:00:00
5Eli2020-11-20 15:15:00