← Back to SQL, page 7

sql-0139 · Medium mid

Can CASE be used inside UPDATE, and how would you give different raises to different departments in one statement?

Yes. SET salary = salary * CASE dept_id WHEN 1 THEN 1.05 WHEN 2 THEN 1.10 ELSE 1 END updates every row in one pass with per-row logic. Add a WHERE clause so rows that should not change are not touched, which avoids needless writes and trigger calls.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
UPDATE employees
SET salary = CAST(salary * CASE dept_id WHEN 1 THEN 1.05 WHEN 2 THEN 1.10 END AS INTEGER)
WHERE dept_id IN (1, 2);
SELECT name, dept_id, salary FROM employees ORDER BY id;
namedept_idsalary
Asha1126000
Ravi199750
Mei199750
Tom277000
Sara268200
Diego355000
LenaNULLNULL
Omar348000

Engineering got 5 percent and Sales 10 percent. Support and Lena (no department) were excluded by WHERE, so their rows were not rewritten at all. Without the WHERE, the ELSE 1 branch would still keep values the same, but every row would be written, firing triggers and generating log volume for no change.

CAST(... AS INTEGER) keeps the column integer, because multiplying by 1.05 gives a real number. One UPDATE with CASE is also atomic: there is no moment where Engineering has its raise and Sales does not.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

UPDATE employees
SET salary = CAST(salary * CASE dept_id WHEN 1 THEN 1.05 WHEN 2 THEN 1.10 END AS INTEGER)
WHERE dept_id IN (1, 2);
SELECT name, dept_id, salary FROM employees ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • CASE works in SET expressions
  • One statement applies different logic per row
  • WHERE limits writes to rows that change
  • A single statement is atomic

Common mistakes

  • Running separate UPDATEs and leaving partial state on failure
  • Updating every row with a no-op ELSE

Follow-ups

  • How would you update from a lookup table of raise percentages instead?
  • What happens to NULL salaries?

Tips

  • Mention atomicity and needless writes