Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL INSERT INTO SELECT Statement
Next article icon

PL/SQL INSERT INTO SELECT

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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_idemp_namestatus
1Alice JohnsonActive
2Bob SmithInactive
3Charlie BrownActive
4Diana PrinceInactive

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:

emp_idemp_namestatus


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_idemp_namestatus
4Bob MartinInactive

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_idemp_namestatus
2Bob SmithInactive
4Diana PrinceInactive

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


Next Article
SQL INSERT INTO SELECT Statement

M

muditgu1tud
Improve
Article Tags :
  • Databases
  • PL/SQL

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences