Welcome to The Coding College! In this tutorial, we’ll explore the SQL INSERT INTO SELECT Statement, a powerful way to copy data from one table to another. By the end of this guide, you’ll understand its syntax, use cases, and practical examples to use it effectively in your database operations.
What Is SQL INSERT INTO SELECT?
The SQL INSERT INTO SELECT statement is used to copy data from one table into another table. Unlike the SELECT INTO statement, which creates a new table, the INSERT INTO SELECT statement requires an existing target table.
Syntax
INSERT INTO target_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;
Key Features
Requires Existing Target Table: The target_table must already exist in the database.
Customizable Columns: Allows selective insertion of specific columns into the target table.
Conditional Data Copy: You can filter rows from the source table using a WHERE clause.
Example Tables
Table 1: employees
employee_id
name
salary
department
1
Alice
70000
HR
2
Bob
80000
IT
3
Charlie
65000
HR
4
Diana
90000
IT
Table 2: high_earners (Target Table)
employee_id
name
salary
Examples
Example 1: Insert All Data
Query: Copy All Employee Data to high_earners
INSERT INTO high_earners (employee_id, name, salary)
SELECT employee_id, name, salary
FROM employees;
Result
employee_id
name
salary
1
Alice
70000
2
Bob
80000
3
Charlie
65000
4
Diana
90000
Example 2: Insert Data with Conditions
Query: Insert Employees with Salary > 75000
INSERT INTO high_earners (employee_id, name, salary)
SELECT employee_id, name, salary
FROM employees
WHERE salary > 75000;
Result
employee_id
name
salary
2
Bob
80000
4
Diana
90000
Example 3: Insert Data with Modified Columns
Query: Add Employees from IT Department with Modified Salary
INSERT INTO high_earners (employee_id, name, salary)
SELECT employee_id, name, salary * 1.1
FROM employees
WHERE department = 'IT';
Explanation
The salary is increased by 10% during the insertion process.
Result
employee_id
name
salary
2
Bob
88000
4
Diana
99000
Advanced Use Cases
Combining Data from Multiple Tables
INSERT INTO high_earners (employee_id, name, salary)
SELECT e.employee_id, e.name, d.avg_salary
FROM employees e
JOIN departments d
ON e.department = d.department
WHERE e.salary > d.avg_salary;
Explanation
This query inserts employee details where their salary exceeds their department’s average salary.
Common Errors
Column Mismatch: Ensure the number of columns in the target_table matches the columns in the SELECT query.
Data Type Mismatch: The data types of columns in the source and target tables should be compatible.
Constraint Violations: Ensure that the data being inserted does not violate constraints like primary keys or foreign keys.
Best Practices
Test Before Execution: Run the SELECT query separately to validate the data being inserted.
Use Explicit Column Names: Avoid SELECT * for better clarity and control.
Backup Data: Always backup critical data before performing insertions.
INSERT INTO SELECT vs SELECT INTO
Feature
INSERT INTO SELECT
SELECT INTO
Table Creation
Requires an existing table.
Creates a new table.
Use Case
Copy data into an existing table.
Copy data and structure to a new table.
Custom Constraints
Constraints and indexes exist beforehand.
No constraints or indexes in new table.
Conclusion
The SQL INSERT INTO SELECT statement is a versatile tool for transferring data between tables. Whether you’re copying data for reporting, backups, or creating new datasets, this feature ensures efficiency and flexibility.
For more SQL tutorials and resources, visit The Coding College, your trusted partner for mastering coding and programming!