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:
PL/SQL CASE Statement
Next article icon

PL/SQL DELETE Statement

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

In PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively removing data that is no longer needed or that meets specific criteria.

PL/SQL DELETE Statement

The DELETE statement in the PL/SQL is designed to remove records from a specified table based on the condition defined in the WHERE clause. An operation targets only those rows that satisfy the condition and leaves the rest of the table intact.

If no condition is used, all the rows in the table will be deleted. This statement is the fundamental tool in PL/SQL for managing the lifecycle of the data within the database. The correct usage of the DELETE statement ensures that the database remains clean, well-organized, and consistent.

Syntax:

DELETE FROM table_name
WHERE condition;

Key terms:

  • table_name is the name of the table from which we want to delete the records.
  • condition specifies which row needs to be deleted. If the condition is not used, all rows in table will deleted.

Employee Table

  • In the below code, CREATE TABLE statement is used to create the employeeDetails table with respective columns.
  • INSERT INTO statement is used to insert the rows into the employeeDetails table, the values are correspond to the employee_id, name and department columns.

Query:

CREATE TABLE employeeDetails (
employee_id NUMBER(10) PRIMARY KEY,
name VARCHAR2(10),
department VARCHAR2(20)
);

-- Insert a single row
INSERT INTO employeeDetails (employee_id, name, department)
VALUES (101, 'John Smith', 'HR');

-- Insert another row
INSERT INTO employeeDetails (employee_id, name, department)
VALUES (102, 'Jane Doe', 'IT');

-- Insert a third row
INSERT INTO employeeDetails (employee_id, name, department)
VALUES (103, 'Mike Brown', 'Finance');

-- Insert a fourth row
INSERT INTO employeeDetails (employee_id, name, department)
VALUES (104, 'Lisa Wong', 'HR');

--Insert a fifth row
INSERT INTO employeeDetails (employee_id, name, department)
VALUES (105, 'Alice Grey', 'Marketing');

Output:

employee_id

name

department

101

John Smith

HR

102

Jane Doe

IT

103

Mike Brown

Finance

104

Lisa Wong

HR

105

Alice Grey

Marketing

Department Table

  • In the below code, CREATE TABLE statement is used to create the department table with respective columns.
  • INSERT INTO statement is used to insert the rows into the department table, the values are corresponded to the department, and name columns.

Query:

CREATE TABLE department (
department VARCHAR2(20),
name VARCHAR2(10)
);
--Insert a single row
INSERT INTO department (department, name)
VALUES ('HR', 'John Smith');

--Insert another row
INSERT INTO department (department, name)
VALUES ('Finance', 'Mike Brown');

--Insert third row
INSERT INTO department (department, name)
VALUES ('IT', 'Jane Doe');

Output:

department

name

HR

John Smith

Finance

Mike Brown

IT

Jane Doe

Example 1: Using One Condition in the DELETE Statement

Let us explore the example of using the DELETE statement with the single condition to remove specific rows from employeeDetails table. Suppose we want to delete the record of employee whose department is IT. We can achieve this with the help of DELETE statement with the condition on department column.

Query:

DELETE FROM employeeDetails
WHERE department = 'IT';

Output:

employee_id

name

department

101

John Smith

HR

103

Mike Brown

Finance

104

Lisa Wong

HR

105

Alice Grey

Marketing

Explanation:

  • DELETE FROM employeeDetails: This part of statement is specify that we are delete rows from employeeDetails table.
  • WHERE department = 'IT': This condition target only the rows where department is IT. In above employeeDetails table, this condition matches the row for the employee with the employee_id = 102 (Jane Doe).
  • Hence, the row for Jane Doe, whose department was IT, is deleted. The rest of the rows in employeeDetails table remain unchanged.

Example 2: Using Two Conditions in the DELETE Statement

In this example, we will see the how to use DELETE statement with the multiple conditions to remove the specific rows from employeeDetails table. Suppose we want to delete the record of employee who works in HR department and has employee_id of 101. This is done with the help of DELETE statement with the two conditions combined with the AND operator.

Query:

DELETE FROM employeeDetails
WHERE department = 'HR' AND employee_id = 101;

Output:

employee_id

name

department

102

Jane Doe

IT

103

Mike Brown

Finance

104

Lisa Wong

HR

105

Alice Grey

Marketing

Explanation:

  • In the above code, DELETE FROM employeeDetails: This part of the statement is specify that deletion is to be performed on employeeDetails table.
  • WHERE department = 'HR' AND employee_id = 101: This condition is ensure that only row where department is HR and the employee_id is 101 will deleted from the employeeDetails table.
  • The role for John Smith, who is worked in HR department and he had employee_id = 101 is deleted. Other rows, including those are in the HR department but it was in different employee_id values, remains unchanged.

Example 3: Using EXISTS Clause

The EXISTS clause in the SQL (Structured Query Language) is used to check the existence of the rows in the subquery. When the DELETE statement is used with a JOIN or subquery, it allows for the deletion of rows from a table based on the presence of related rows in another table or even within the same table.

This is useful for enforcing relationships between tables or conditions within a single table. Suppose we want to delete the employees from the employeeDetails table who are work in the departments that has no longer exist in department table.

Query:

DELETE FROM employeeDetails e
WHERE EXISTS (
SELECT 1 FROM department d
WHERE d.department = e.department
);

Output:

employee_id

name

department

105

Alice Grey

Marketing

Explanation:

  • In the above code, DELETE FROM employeeDetails e: specifies that the decision is to perform on employeeDetails table with e as its alias.
  • WHERE EXISTS: The EXISTS clause is used to check the subquery and return any rows. If the subquery return any rows, then condition is true, and the corresponding rows in the employeeDetails will be deleted.
  • Hence, the DELETE statement removes all the employees whose departments exist in department table i.e. HR, IT, Finance. The remaining row is Alice Grey, which is in Marketing department. It does not exist in department table.

Conclusion:

In conclusion, DELETE statement in the PL/SQL is the fundamental tool for the managing the data within the relational database. It allows for the precise removal of the records based on the certain conditions and it as essential part of the database maintenance and the data management.

By the understanding of the DELETE statement effectively, we can ensure that the our database remain accurate, efficient and free of outdated data or unnecessary data.


Next Article
PL/SQL CASE Statement
author
mjagan716
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • SQL DELETE Statement
    The SQL DELETE statement is one of the most commonly used commands in SQL (Structured Query Language). It allows you to remove one or more rows from the table depending on the situation. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the tabl
    4 min read
  • MySQL DELETE Statement
    In DBMS, CRUD operations (Create, Read, Update, Delete) are essential for effective data management. The Delete operation is crucial for removing data from a database. This guide covers the MySQL DELETE statement, exploring its syntax and providing examples. Understanding how DELETE works helps ensu
    6 min read
  • PL/SQL INSERT Statement
    The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
    3 min read
  • PL/SQL CASE Statement
    PL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. The PL/SQL CASE statement is a powerfu
    4 min read
  • PL/SQL CREATE TABLE Statement
    PL/SQL CREATE TABLE statement is a fundamental aspect of database design and allows users to define the structure of new tables, including columns, data types, and constraints. This statement is crucial in organizing data effectively within a database and providing a blueprint for how data should be
    4 min read
  • PL/SQL Statement level Triggers
    Statement-level triggers in Oracle databases execute actions for each transaction, responding to various database events like DML and DDL statements, system events, and user interactions. They act as programmed responses to specific table events, enhancing database management and automation. Stored
    4 min read
  • SQLAlchemy Core - Delete Statement
    In this article, we are going to see how to use the DELETE statement in SQLAlchemy against a PostgreSQL database in python. Creating table for demonstration: Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as sh
    2 min read
  • SQL CASE Statement
    The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
    4 min read
  • PL/SQL UPDATE Statement
    The UPDATE statement in the PL/SQL(Procedural Language/ Structural Query Language) is the powerful SQL (Structured Query Language) command used to modify the existing data in the database table. In this article, we will explain the PL/SQL UPDATE Statement, its syntax, and examples in detail. PL/SQL
    7 min read
  • SQL USE Database Statement
    SQL(Structured Query Language) is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. It is flexible and user-friendly. In SQL, to interact with the database, the users have to type queries that have certain syntax, a
    2 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