PL/SQL INSERT INTO SELECT
Last Updated : 10 Sep, 2024
In PL/SQL, the INSERT INTO SELECT statement is used to insert data into a table by selecting data from one or more tables. This is a powerful feature for populating tables with data from existing tables or views, making it useful for data migration, reporting, and backup processes.
In this guide, we will learn how to use PL/SQL INSERT INTO SELECT statements with various examples.
PL/SQL INSERT INTO SELECT
The INSERT INTO SELECT
statement allows us to insert data into a table by selecting and retrieving data from another table or query result.
This is especially useful when we need to copy data or aggregate data from multiple sources into a new table. It can handle both the insertion of specific columns and the transformation of data as it is inserted.
Syntax:
INSERT INTO target_table (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM source_table
WHERE condition;
Explanation:
- target_table: The table where data will be inserted.
- column1, column2, column3, ... : It represent the Columns in the target table to receive data.
- source_table: The table from which data will be selected.
- condition: Specifies which rows to select from the source table.
Examples of PL/SQL INSERT INTO SELECT
To understand the INSERT INTO SELECT
functionality in PL/SQL, we’ll use two tables: employees
and archived_employees
. We will copy data from the employees
table to the archived_employees
table for employees who meet specific criteria (e.g., employees who are no longer with the company).
employees table:
The provided query creates an employees
table with three columns: emp_id
, emp_name
, and status
. It then inserts four records into the table, each representing an employee with a unique ID, name, and employment status.
Query:
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR2(50),
status VARCHAR2(20)
);
INSERT INTO employees (emp_id, emp_name, status) VALUES (1, 'Alice Johnson', 'Active');
INSERT INTO employees (emp_id, emp_name, status) VALUES (2, 'Bob Smith', 'Inactive');
INSERT INTO employees (emp_id, emp_name, status) VALUES (3, 'Charlie Brown', 'Active');
INSERT INTO employees (emp_id, emp_name, status) VALUES (4, 'Diana Prince', 'Inactive');
Output:
emp_id | emp_name | status |
---|
1 | Alice Johnson | Active |
2 | Bob Smith | Inactive |
3 | Charlie Brown | Active |
4 | Diana Prince | Inactive |
Explanation:
The output displays the contents of the employees
table after the insertions. It lists four employees with their respective IDs, names, and statuses:
- Alice Johnson and Charlie Brown are marked as Active.
- Bob Smith and Diana Prince are marked as Inactive.
This setup reflects the initial state of the employees
table, showing both active and inactive employees.
archived_employees table:
The query creates a new table named archived_employees
with the same structure as the employees
table, including columns for emp_id
, emp_name
, and status
. This table is intended to store records of employees who are no longer with the company.
Query:
CREATE TABLE archived_employees (
emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR2(50),
status VARCHAR2(20)
);
Output:
Explanation:
The output displays the schema of the archived_employees
table after its creation. Since no data has been inserted yet, the table is empty, but it is ready to store employee records with the same structure as the employees
table.
Example 1: Using the VALUES Keyword
The INSERT INTO
archived_employees
statement uses the VALUES
keyword to insert a new row into the archived_employees
table with explicit values for emp_id
, emp_name
, and status
.
This does not involve data from another table rather, it directly inserts the provided values into the specified columns
INSERT INTO archived_employees (employee_id, name, department, status)
VALUES (4, 'Bob Martin', 'Marketing', 'Inactive');
Output:
emp_id | emp_name | status |
---|
4 | Bob Martin | Inactive |
Explanation:
The output shows that the archived_employees
table now contains one new record with emp_id
4, emp_name
'Bob Martin', and status
'Inactive'. This data was inserted directly using the VALUES
clause.
Example 2: Using the SELECT Statement
The INSERT INTO SELECT
statement is used to copy data from the employees
table to the archived_employees
table.
The query selects all columns for employees with a status of 'Inactive' and inserts them into archived_employees
. This allows for bulk copying of records based on the specified condition.
INSERT INTO archived_employees (employee_id, name, department, status)
SELECT employee_id, name, department, status
FROM employees
WHERE status = 'Inactive';
Output:
emp_id | emp_name | status |
---|
2 | Bob Smith | Inactive |
4 | Diana Prince | Inactive |
Explanation:
The archived_employees
table now includes the records of all employees with the status 'Inactive' from the employees
table. This demonstrates how data can be efficiently transferred between tables based on specific criteria.
Conclusion
The INSERT INTO SELECT statement is a valuable tool in PL/SQL for transferring and manipulating data between tables. It allows us to efficiently move data based on specific conditions or queries, making it an essential feature for database management and operations. By understanding how to use INSERT INTO SELECT, we can streamline data handling processes and enhance the functionality of your database systems
Similar Reads
SQLite INSERT INTO SELECT
SQLite is a lightweight and server-less relational database management system. It requires very minimal configuration which has proven to be very helpful for developers to integrate it into any applications with ease. Due to its server-less architecture, we can use SQLite in various mobile applicati
4 min read
PL/SQL INSERT INTO
PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. It allows us to write complex queries and scripts that include procedural logic, control structures, and error handling. The INSERT INTO statement in PL/SQL is essential for adding new rows of data to tab
6 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
6 min read
PL/SQL SELECT INTO Existing Table
PL/SQL is a programming language that is used alongside SQL for writing procedural code such as stored procedures, functions, triggers, and packages within the Oracle Database. It was developed by Oracle Corporation and is widely used in database programming. PL/SQL is a programming language that ha
5 min read
MySQL INSERT INTO SELECT Statement
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. INSERT INTO SELECT statement i
5 min read
SQL | INSERT INTO Query
In SQL, managing data effectively involves the ability to insert new rows into a table. TheINSERT INTO statement is used to add data to a database table. This statement provides two methods for inserting rows: inserting only values and inserting values with column names. Each method has its own synt
4 min read
PL/SQL SELECT FROM
PL/SQL is Oracle procedural extension for SQL which allows for more powerful and flexible database manipulation. The SELECT statement is one of the most fundamental and widely used commands in SQL. It allows users to retrieve data from one or more tables in a database. In this article, we will learn
5 min read
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
SELECT INTO in MySQL
MySQL is an open-source Relational Database Management System that stores data in a structured format using rows and columns. MySQL is a platform-independent language and can be compiled on any platform. MySQL is generally used for storing, manipulating, and retrieving data in RDBMS by using the SQL
5 min read
MySQL INSERT INTO Statement
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and
6 min read