SQL INSERT SELECT

Article Summary

INSERT SELECT is used to copy data from one table into another. This is very useful for data migration, backups, or transformations. πŸ”Ή Basic Syntax πŸ”Ή Example πŸ”Ή Insert All Columns (If Structure Matches) πŸ”Ή Oracle Notes 🧠 Quick Recap Use Case Example Syntax Copy selected rows INSERT INTO target SELECT ... FROM source Insert […]

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

Was this helpful?