ADVERTISEMENT

SQL INSERT SELECT

INSERT SELECT is used to copy data from one table into another. This is very useful for data migration, backups, or transformations.

๐Ÿ”น Basic Syntax

INSERT INTO target_table (col1, col2, ...)
SELECT col1, col2, ...
FROM source_table
WHERE condition;

๐Ÿ”น Example

-- Copy employees from 'IT' department into backup table
INSERT INTO employees_backup (id, name, department)
SELECT employee_id, name, department
FROM employees
WHERE department = 'IT';

๐Ÿ”น Insert All Columns (If Structure Matches)

INSERT INTO employees_archive
SELECT * FROM employees
WHERE status = 'inactive';

๐Ÿ”น Oracle Notes

  • Works the same way in Oracle.
  • You can also use INSERT /*+ APPEND */ for performance tuning in large loads.

๐Ÿง  Quick Recap

Use CaseExample Syntax
Copy selected rowsINSERT INTO target SELECT ... FROM source
Insert all columnsINSERT INTO target SELECT * FROM source
With condition/filterAdd WHERE clause to the SELECT part

โœ… Ideal for copying, archiving, or transforming data between tables

Close โœ–