Sub topic 11
Subqueries in INSERT, UPDATE and DELETE
Assume you know SELECT, joins, aggregate functions, and the Oracle HR sample schema. This chapter focuses on using a query inside a data manipulation statement (DML). DML statements change table data, so test them in a transaction and verify the affected rows before committing.
A subquery can supply rows to an INSERT, identify rows for an UPDATE, or limit rows removed by a DELETE.
INSERT with a subquery
INSERT INTO ... SELECT copies the result of a query into a table. The selected columns must match the target columns in number and compatible data type.
For example, suppose an archive table stores employees from department 10:
CREATE TABLE employees_archive (
employee_id NUMBER,
first_name VARCHAR2( veinte ),
last_name VARCHAR2(25),
salary NUMBER,
department_id NUMBER
);
INSERT INTO employees_archive
(employee_id, first_name, last_name, salary, department_id)
SELECT employee_id, first_name, last_name, salary, department_id
FROM employees
WHERE department_id = 10;
COMMIT;The SELECT runs first conceptually. Its result becomes the rows inserted into employees_archive. In real work, create the archive table with column sizes that match your source table. For example, VARCHAR2(20) must be written with a numeric size such as VARCHAR2(20), not a word.
You can also use a subquery to choose a value:
INSERT INTO department_summary (department_id, average_salary)
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id;UPDATE with a correlated subquery
A correlated subquery refers to a column from the row being changed. This lets each row use a value calculated for its own department.
The following gives a 5 percent increase to employees paid below their department average:
UPDATE employees e
SET e.salary = e.salary * 1.05
WHERE e.salary < (
SELECT AVG(i.salary)
FROM employees i
WHERE i.department_id = e.department_id
);
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id IS NOT NULL;
ROLLBACK;The aliases are important. e identifies the row being updated. i identifies rows read by the subquery. Without the correlation condition, the average would be the company-wide average instead of the employee's department average.
Check the number of updated rows before using COMMIT. Use ROLLBACK while experimenting.
DELETE with EXISTS and NOT EXISTS
A subquery can protect a DELETE from removing rows that are still needed. This statement removes departments with no employees:
DELETE FROM departments d
WHERE NOT EXISTS (
SELECT 1
FROM employees e
WHERE e.department_id = d.department_id
);
ROLLBACK;NOT EXISTS is true when the subquery finds no matching row. The selected expression, 1, is irrelevant. Oracle only needs to know whether a row exists.
This is often safer than NOT IN when the subquery column might contain NULL. A NULL can make a NOT IN condition evaluate to unknown, preventing expected matches.
Choosing the DML pattern
| Task | Common pattern | Main question |
|---|---|---|
| Copy query results | `INSERT ... SELECT` | Do the columns match? |
| Change rows using related data | `UPDATE ... WHERE` with a subquery | Is the subquery correlated correctly? |
| Remove unmatched rows | `DELETE ... WHERE NOT EXISTS` | Could `NULL` affect the condition? |
Exercise
Write a DELETE statement that removes employees whose department does not exist in DEPARTMENTS, while avoiding the NULL problem associated with NOT IN.
Show answer
DELETE FROM employees e WHERE NOT EXISTS ( SELECT 1 FROM departments d WHERE d.departmentid = e.departmentid );