Sub topic 3
Copying Rows with `INSERT ... SELECT`
Assume you know basic SELECT queries, table definitions, and primary key, foreign key, and NOT NULL constraints. This chapter uses the dept_demo, emp_demo, and emp_archive tables from the tutorial setup.
INSERT ... SELECT inserts the rows returned by a query into another table. It is useful for archiving, staging, transforming, and copying data without sending one INSERT statement per row.
The basic pattern
The target column list determines where each selected value goes. The query must return the same number of values, in a compatible order.
INSERT INTO emp_archive (emp_id, emp_name, dept_id, salary, status)
SELECT emp_id, emp_name, dept_id, salary, status
FROM emp_demo
WHERE status = 'INACTIVE';Oracle evaluates the SELECT, then inserts each returned row into emp_archive. If the query returns no rows, the statement succeeds but inserts nothing.
Always write the target column list unless you fully control both table definitions. Omitting it makes the statement depend on the target table's physical column order.
INSERT INTO emp_archive (emp_id, emp_name, dept_id, salary, status)
SELECT emp_id,
emp_name,
dept_id,
salary * 1.05,
status
FROM emp_demo
WHERE dept_id = 20;Expressions, constants, functions, and aliases can appear in the query. The selected values must still satisfy the target table's constraints.
Copying and transforming data
The source and target tables do not need to have the same names or identical structures. For example, this statement copies selected columns into a reporting table:
CREATE TABLE emp_salary_report (
emp_id NUMBER,
employee VARCHAR2(50),
annual_salary NUMBER(10,2)
);
INSERT INTO emp_salary_report (emp_id, employee, annual_salary)
SELECT emp_id,
emp_name,
salary * 12
FROM emp_demo
WHERE salary IS NOT NULL;The query can join other tables, group rows, or use a subquery. Each result row is checked against the target table's constraints.
| Source query result | Target action |
|---|---|
| One returned row | One target row is attempted |
| No returned rows | No rows are inserted |
| Duplicate key value | The statement fails with a constraint error |
| `NULL` for a required column | The statement fails with a `NOT NULL` error |
Transactions and duplicate rows
INSERT ... SELECT is one DML statement. Oracle normally holds its changes in the current transaction until you issue COMMIT or ROLLBACK.
INSERT INTO emp_archive (emp_id, emp_name, dept_id, salary, status)
SELECT emp_id, emp_name, dept_id, salary, status
FROM emp_demo
WHERE status = 'INACTIVE';
SELECT COUNT(*) AS archived_rows
FROM emp_archive;
COMMIT;The SELECT used for the insert can return duplicate values even when the source table has no duplicates. If the target has a primary key or unique constraint, those duplicates can cause ORA-00001. Use DISTINCT, grouping, or a more selective query only when that matches the required business rule. Do not use DISTINCT merely to hide an incorrect join.
How the statement works
A useful workflow is:
- Write and inspect the source
SELECT. - Compare its columns with the target column list.
- Check whether existing target rows could conflict.
- Run the
INSERT ... SELECT. - Review the affected row count.
- Commit only after validation.
Exercise
How would you archive every employee whose status is TERMINATED into emp_archive while preserving all five employee columns?
Show answer
Use INSERT INTO emparchive (empid, empname, deptid, salary, status) SELECT empid, empname, deptid, salary, status FROM empdemo WHERE status = 'TERMINATED';