← Back to SQL, page 17

sql-0329 · Medium mid

A pay-rise script set most employees' salaries to NULL. Explain why, and fix it.

An UPDATE without a WHERE clause touches every row, and the correlated subquery returns NULL for employees with no matching raise. Restrict the update with WHERE EXISTS (or an UPDATE ... FROM join) so only employees with a raise change.

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');
CREATE TABLE raises (emp_id INTEGER PRIMARY KEY, new_salary INTEGER);
INSERT INTO raises VALUES (4, 75000), (5, 66000);
CREATE TABLE employees_copy AS SELECT * FROM employees;

The query as written:

sql
UPDATE employees SET salary = (SELECT new_salary FROM raises WHERE raises.emp_id = employees.id);
SELECT id, name, salary FROM employees ORDER BY id;
idnamesalary
1AshaNULL
2RaviNULL
3MeiNULL
4Tom75000
5Sara66000
6DiegoNULL
7LenaNULL
8OmarNULL

The subquery is evaluated per row. For Tom and Sara it finds a raise; for everyone else it finds no row, and a scalar subquery with no row yields NULL. Every row without a raise, six in all, now holds NULL; Lena's salary was already NULL, so five real salaries were lost.

A corrected version:

sql
UPDATE employees_copy SET salary = (SELECT new_salary FROM raises WHERE raises.emp_id = employees_copy.id)
WHERE EXISTS (SELECT 1 FROM raises WHERE raises.emp_id = employees_copy.id);
SELECT id, name, salary FROM employees_copy ORDER BY id;
idnamesalary
1Asha120000
2Ravi95000
3Mei95000
4Tom75000
5Sara66000
6Diego55000
7LenaNULL
8Omar48000

The fix ran on an untouched copy of the table. Only rows with a raise are updated, and the other salaries stay as they were. SQLite 3.33+ and PostgreSQL also accept UPDATE employees SET salary = r.new_salary FROM raises r WHERE r.emp_id = employees.id, which reads more naturally. Before any bulk update, run the same logic as a SELECT and check the row count inside a transaction.

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');
CREATE TABLE raises (emp_id INTEGER PRIMARY KEY, new_salary INTEGER);
INSERT INTO raises VALUES (4, 75000), (5, 66000);
CREATE TABLE employees_copy AS SELECT * FROM employees;

UPDATE employees SET salary = (SELECT new_salary FROM raises WHERE raises.emp_id = employees.id);
SELECT id, name, salary FROM employees ORDER BY id;

UPDATE employees_copy SET salary = (SELECT new_salary FROM raises WHERE raises.emp_id = employees_copy.id)
WHERE EXISTS (SELECT 1 FROM raises WHERE raises.emp_id = employees_copy.id);
SELECT id, name, salary FROM employees_copy ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • UPDATE without WHERE changes every row
  • Scalar subqueries with no match give NULL
  • Restrict with WHERE EXISTS or UPDATE ... FROM
  • Preview bulk updates as SELECTs in a transaction

Common mistakes

  • Trusting that the subquery limits which rows change
  • Running bulk updates outside a transaction

Follow-ups

  • How would you undo this if there were no backup?
  • How does UPDATE ... FROM behave when several rows match?

Tips

  • Preview row counts before updating