Sub topic 5
UPDATE with Subqueries and Correlated Values
This chapter assumes you can write basic SELECT statements with WHERE, understand primary and foreign keys, and use the emp_demo and dept_demo tables from the earlier examples. You should also know that UPDATE changes rows in the current transaction until you commit or roll back.
A subquery is a query inside another SQL statement. In an UPDATE, a subquery can select which rows to change, calculate a new value, or both.
Filtering rows with a subquery
Suppose every employee in Sales should receive a 5 percent raise. The department name is stored in dept_demo, so use a subquery to find its key:
UPDATE emp_demo
SET salary = salary * 1.05
WHERE dept_id IN (
SELECT dept_id
FROM dept_demo
WHERE dept_name = 'Sales'
);
COMMIT;The inner query returns department IDs. The outer UPDATE changes employees whose dept_id matches one of those IDs.
IN is useful when the subquery can return several values. If you expect exactly one value, you can use = instead. The subquery must then return at most one row.
Calculating a value with a subquery
A subquery in the SET clause can calculate a replacement value. This example sets each employee's salary to the average salary in that employee's department:
UPDATE emp_demo e
SET e.salary = (
SELECT AVG(e2.salary)
FROM emp_demo e2
WHERE e2.dept_id = e.dept_id
)
WHERE e.dept_id IS NOT NULL;The inner query references e.dept_id, which belongs to the current row of the outer update. This is a correlated subquery. Oracle evaluates it using the department of each employee being updated.
A scalar subquery in the SET clause must return one value for each outer row. An aggregate such as AVG naturally returns one value. A subquery that returns multiple rows raises ORA-01427: single-row subquery returns more than one row.
The update may also set a column to NULL when the subquery finds no matching rows. Use a WHERE condition or NVL when that result is not acceptable:
UPDATE emp_demo e
SET e.salary = NVL((
SELECT AVG(e2.salary)
FROM emp_demo e2
WHERE e2.dept_id = e.dept_id
), e.salary)
WHERE e.dept_id IS NOT NULL;NVL keeps the current salary if the calculated value is null.
Common errors and safer checks
Always inspect the target rows before running a significant update:
SELECT e.emp_id, e.salary, d.dept_name
FROM emp_demo e
JOIN dept_demo d ON d.dept_id = e.dept_id
WHERE d.dept_name = 'Sales';Then perform the update and check the affected row count in your SQL tool. If the result is wrong, use ROLLBACK before COMMIT.
A correlated subquery can be slower than a join-based design on a large table. Check the execution plan and indexes when performance matters. For correctness, ensure the correlation condition identifies the intended group and does not accidentally create a many-row scalar result.
Exercise
Write an UPDATE that adds 500 to the salary of employees whose salary is below the average salary for their own department.
Show answer
UPDATE empdemo e SET e.salary = e.salary + 500 WHERE e.salary < ( SELECT AVG(e2.salary) FROM empdemo e2 WHERE e2.deptid = e.deptid );