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:
How to Find Duplicate Records in SQL?
Next article icon

How to Find Duplicate Rows in PL/SQL

Last Updated : 06 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Finding duplicate rows is a widespread requirement when dealing with database analysis tasks. Duplicate rows often create problems in analyzing tasks. Detecting them is very important. PL/SQL is a procedural extension for SQL. We can write custom scripts with the help of PL/SQL and thus identifying duplicate data eventually becomes very easy.

PL/ SQL also provides us with various other methods to easily solve our duplicate data problem. In this article, we will explore multiple methods to identify duplicate rows in PL/SQL, including a basic iterative approach and using the powerful GROUP BY clause. Each method will be demonstrated with examples and explanations, providing you with the knowledge to efficiently locate and manage duplicate data in your PL/SQL environment.

Methods to Find Duplicate Rows in PL/SQL

We need to implement the COUNT() function, to find the duplicate rows in the table. We have to count several rows that appear more than once in a table. We have to keep the count of each row of the table. If any row appears to have a count of more than 1, then it will be considered a duplicate row. Some common approaches are as follows:

Table of Content

  • Using Naive/Basic
  • Using GROUP BY Clause

Demo SQL Database

In this a on "finding duplicate rows in PL/SQL", we will use the following table for examples.

table--gfg
Table - geeksforgeeks

Create Table:

PL/SQL
CREATE TABLE geeksforgeeks  (      id NUMBER ,      name VARCHAR2(50),      rank NUMBER  ); INSERT INTO geeksforgeeks (id, name, rank) VALUES (108, 'Vishu', 135);  INSERT INTO geeksforgeeks (id, name, rank) VALUES (109, 'Ayush', 136);  INSERT INTO geeksforgeeks (id, name, rank) VALUES (110, 'Sumit', 137);  INSERT INTO geeksforgeeks (id, name, rank) VALUES (109, 'Ayush', 136);  INSERT INTO geeksforgeeks (id, name, rank) VALUES (108, 'Vishu', 135); 

After running the above commands, the geeksforgeeks table will contain duplicate rows, which we will attempt to identify using PL/SQL.

Method 1: Basic Approach Using COUNT()

In this method, we will use most common method to find the duplicate values from the table. In short, we will use the most basic approach.

We will create some unique combinations and iterate through each combination. If we find that any combination has more than 1 as count, then we will consider this a duplicate.

Query:

DECLARE     --varibale declaration     dupli_count INTEGER; BEGIN     --we are iterating over distinct rows only.     FOR i IN (SELECT distinct id, name, rank FROM geeksforgeeks) LOOP         --if the count has value more than 1, then we will consider it as duplicate          SELECT COUNT(*) INTO dupli_count         FROM geeksforgeeks         WHERE id = i.id AND name = i.name AND rank = i.rank;          IF dupli_count > 1 THEN             DBMS_OUTPUT.PUT_LINE('duplicate rows ID: ' || i.id ||  ' and name: ' || i.name || ' and rank: ' || i.rank || ' having count: ' || dupli_count);         END IF;     END LOOP; END;

Output:

group-by
Basic Approach

Explanation:

  • The loop iterates over each unique combination of id, name, and rank.
  • For each unique row, we count its occurrences in the table.
  • If any row count exceeds 1, it is printed as a duplicate row.

Method 2: Using the GROUP BY Clause

In this method, we will use the GROUP BY Clause to find the duplicate rows from our table. The GROUP BY Clause groups the rows based on some specified criteria.

Query:

DECLARE     --you can define any varibale (if required), in this approach we do not need any so its empty BEGIN   FOR i IN (     SELECT id, name, rank, COUNT(*) as c     FROM geeksforgeeks     GROUP BY id, name, rank     HAVING COUNT(*) > 1   ) LOOP     DBMS_OUTPUT.PUT_LINE('duplicate rows ID: ' || i.id ||  ' and name: ' || i.name || ' and rank: ' || i.rank || ' having count: ' || i.c);   END LOOP; END;

Output:

group-by
Using GROUP BY Clause

Explanation:

  • The GROUP BY clause groups rows by id, name, and rank.
  • The COUNT(*) function within the query counts the number of times each unique combination appears.
  • The HAVING clause filters rows where the count is greater than 1, identifying only duplicates

Conclusion

Overall, duplicate data detection is very common in data analysis tasks. We can identify duplicate data very easily in PL/SQL. We can use the GROUP BY Clause to create unique combinations of rows and perform operations over it very easily. We have seen a basic approach as well as an approach that includes the GROUP BY Clause. The GROUP BY Clause makes it very easy to detect duplicate data in a PL/SQL environment. Now you have a good understanding of detecting duplicate data in a PL/SQL environment. Now you can write queries related to it and get the desired result.


Next Article
How to Find Duplicate Records in SQL?

V

vishuvaishnav3001
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • How to Find Duplicate Records in SQL?
    To find duplicate records in SQL, we can use the GROUP BY and HAVING clauses. The GROUP BY clause allows us to group values in a column, and the COUNT function in the HAVING clause shows the count of the values in a group. Using the HAVING clause with a condition of COUNT(*) > 1, we can identify
    3 min read
  • How to Delete Duplicate Rows in PL/SQL?
    Inconsistencies and inefficiencies in data management are frequently caused by duplicate rows in a database table. Eliminating duplicate rows is a typical PL/SQL activity to maintain data integrity and improve database performance. This article will guide you on how to remove duplicated rows in PL/S
    4 min read
  • How to Fetch Duplicate Rows in a Table?
    Identifying duplicate rows in a database table is a common requirement, especially when dealing with large datasets. Duplicates can arise due to data entry errors, system migrations, or batch processing issues. In this article, we will explain efficient SQL techniques to identify and retrieve duplic
    3 min read
  • How to delete duplicate rows in SQLite?
    SQLite is an open-source and serverless database system that does not require any server to perform various queries also it is widely used in the development of embedded software like television and mobile phones Sometimes it might happen that we by mistake insert multiple times similar data into ta
    3 min read
  • How to Show Database in PL/SQL
    PL/SQL is the Procedural Language/Structured Query Language and serves as a procedural language built-in extension to SQL language, which allows seamless integration of procedural constructs with SQL. One of the most common functions of a DBMS is the retrieval of information about databases which is
    4 min read
  • Find Duplicates in MS SQL Server
    Finding duplicate values in a database is a common task when managing data integrity. In SQL, several methods can be employed to identify and handle duplicate entries. In this article, We will explore two effective techniques for locating duplicates using SQL queries: the GROUP BY clause and the ROW
    4 min read
  • How to Delete Duplicate Rows in MySQL?
    Duplicate rows are a common problem in MySQL databases. Duplicate rows can cause problems with data accuracy and integrity. They can also make it difficult to query and analyze data. Ways to Delete Duplicate Rows in MySQLThere are a few different ways to delete duplicate rows from tables in MySQL: U
    4 min read
  • How to find duplicate values in a list in R
    In this article, we will see how to find duplicate values in a list in the R Programming Language in different scenarios. Finding duplicate values in a ListIn R, the duplicated() function is used to find the duplicate values present in the R objects. This function determines which elements of a List
    3 min read
  • How to Find and Remove Duplicates in Excel
    Removing duplicates in Excel is essential when cleaning up data to ensure accuracy and avoid redundancy. Whether you’re working with small datasets or large spreadsheets, Excel provides built-in tools and methods to help you identify and remove duplicates effectively. This guide will walk you throug
    9 min read
  • SQL Query to Find Duplicate Names in a Table
    Duplicate records in a database can create confusion, generate incorrect results, and waste storage space. It’s essential to identify and remove duplicates to maintain data accuracy and database performance. In this article, we’ll discuss the reasons for duplicates, how to find duplicate records in
    3 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