Trending
Diphtheria outbreak: What to know as death toll hits 1,384 in two years is trending now UK Imposes Emergency Restrictions Following Disease Outbreak In Fishery Facility is trending now DR Congo Ebola Outbreak Still Spreading Fast In Key Province -WHO is trending now 11 African countries record 1,153 mpox cases in six weeks is trending now Strange 'centaur' object between Saturn and Uranus has mysterious, changing rings, James … is trending now New species of 240-million-year-old prehistoric animal could reshape dinosaur origins tim… is trending now The Roman telescope has enough gas for 22 years, double NASA’s expectations is trending now International Scientists Uncover Reason Behind Slowdown In Star Formation is trending now Hansi Flick defends Barcelona's Ballon d'Or campaign for Lamine Yamal ahead of Racing San… is trending now Video: Liverpool knock out Tottenham and reach the League Cup last 16 is trending now Raheem Sterling admits guilt and could face a lengthy prison sentence is trending now Howard Webb opens the black box on the Manchester derby scandal is trending now Diphtheria outbreak: What to know as death toll hits 1,384 in two years is trending now UK Imposes Emergency Restrictions Following Disease Outbreak In Fishery Facility is trending now DR Congo Ebola Outbreak Still Spreading Fast In Key Province -WHO is trending now 11 African countries record 1,153 mpox cases in six weeks is trending now Strange 'centaur' object between Saturn and Uranus has mysterious, changing rings, James … is trending now New species of 240-million-year-old prehistoric animal could reshape dinosaur origins tim… is trending now The Roman telescope has enough gas for 22 years, double NASA’s expectations is trending now International Scientists Uncover Reason Behind Slowdown In Star Formation is trending now Hansi Flick defends Barcelona's Ballon d'Or campaign for Lamine Yamal ahead of Racing San… is trending now Video: Liverpool knock out Tottenham and reach the League Cup last 16 is trending now Raheem Sterling admits guilt and could face a lengthy prison sentence is trending now Howard Webb opens the black box on the Manchester derby scandal is trending now
Shipping Container

PostgreSQL: How to Delete all duplicate rows Except one

I have already written a similar article to delete duplicate records in SQL Server and MySQL. Here, You can also access that articles. Delete all duplicate rows in MySQL Delete all duplicates rows except one in SQL Server Recently, I got one request for one script to delete duplicate records in PostgreSQL. Most of the […]

I have already written a similar article to delete duplicate records in SQL Server and MySQL.
Here, You can also access that articles.

Delete all duplicate rows in MySQL

Delete all duplicates rows except one in SQL Server

Recently, I got one request for one script to delete duplicate records in PostgreSQL.
Most of the Database Developers have such a requirement to delete duplicate records from the Database.

Like SQL Server, ROW_NUMBER() PARTITION BY is also available in PostgreSQL.
I have prepared this script, using simple inner query with the use of ROW_NUMBER() PARTITION BY clause.

Create a sample table:

CREATE TABLE tbl_RemoveDuplicate
(	
	ID INTEGER PRIMARY KEY
	,Name VARCHAR(150)
);

Insert few duplicate records:

INSERT INTO tbl_RemoveDuplicate VALUES
(1,'ABC'),(2,'XYZ')
,(3,'XYZ'),(4,'RFQ')
,(5,'PQR'),(6,'EFG')
,(7,'EFG'),(8,'ABC');

Except one, Delete all duplicate records:

DELETE FROM tbl_RemoveDuplicate
WHERE ID IN 
(SELECT ID
FROM (SELECT id,
	     ROW_NUMBER() OVER (partition BY Name ORDER BY ID) AS RowNumber
     FROM tbl_RemoveDuplicate) AS T
WHERE T.RowNumber > 1);

Check the result:

SELECT *FROM tbl_RemoveDuplicate;
 id | name
----+------
  1 | ABC
  2 | XYZ
  4 | RFQ
  5 | PQR
  6 | EFG
(5 rows)
View original source →

Related