2-15. Finding and Removing Duplicate Table Rows

Problem

You have found that for some reason your database contains a table that has duplicate records. You are working with a database that unfortunately does not use primary key values, so you must manually enforce data integrity. You need a way to remove the duplicate records. However, any query you write to remove one record will also remove its duplicate.

Solution

The solution to this issue involves two steps. First you need to query the database to find all duplicate rows, and then you need to run a statement to delete one of each duplicate record that is found.

The following code block queries the EMPLOYEES table for duplicate rows. When a duplicate is found, it is returned along with a count ...

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.