Full answer
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:
UPDATE employees SET salary = (SELECT new_salary FROM raises WHERE raises.emp_id = employees.id);
SELECT id, name, salary FROM employees ORDER BY id;| id | name | salary |
|---|---|---|
| 1 | Asha | NULL |
| 2 | Ravi | NULL |
| 3 | Mei | NULL |
| 4 | Tom | 75000 |
| 5 | Sara | 66000 |
| 6 | Diego | NULL |
| 7 | Lena | NULL |
| 8 | Omar | NULL |
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:
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;| id | name | salary |
|---|---|---|
| 1 | Asha | 120000 |
| 2 | Ravi | 95000 |
| 3 | Mei | 95000 |
| 4 | Tom | 75000 |
| 5 | Sara | 66000 |
| 6 | Diego | 55000 |
| 7 | Lena | NULL |
| 8 | Omar | 48000 |
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.
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