Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2019 September PostgreSQL: How to Delete all duplicate rows Except one

PostgreSQL: How to Delete all duplicate rows Except one

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

1
2
3
4
5
CREATE TABLE tbl_RemoveDuplicate
(
ID INTEGER PRIMARY KEY
,Name VARCHAR(150)
);

Insert few duplicate records:

1
2
3
4
5
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:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
8
9
SELECT *FROM tbl_RemoveDuplicate;
id | name
----+------
1 | ABC
2 | XYZ
4 | RFQ
5 | PQR
6 | EFG
(5 rows)

Sep 17, 2019Anvesh Patel
SQL Puzzle: SQL Advance Query - Find the Order basis on thier Status and Step
Comments: 1
  1. Man
    March 2, 2020 at 4:04 am

    This is the only answer that worked for me. It did help me a lot, thanks.

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

September 17, 2019 1 Comment PostgreSQLAnvesh Patel, database, database research and development, dbrnd, delete duplicate, duplicate record, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, records
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....