Find duplicate rows in SQL

Jonathan Brotto 420 Reputation points
2024-07-30T18:52:30.0133333+00:00

I don't mind the approach but was I was looking for a PO within my database and found duplicate rows. Like, have a way to catch these.

Community Center | Not monitored
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2024-07-30T20:23:46.1533333+00:00

    What do you mean "catch"?

    Anyway, to delete duplicate rows, this is the typical solution:

    ; WITH numbering AS (
         SELECT *, rn = row_number() OVER(PARTITION BY keycol ORDER BY something DESC) 
         FROM  tbl
    )
    DELETE numbering
    WHERE rn > 1
    

    In the PARTITION BY column, you list the column(s) where you don't want duplicates. In the ORDER BY clause you have the criteria for which row to keep. I added DESC, since commonly it is a date column, and you want to keep the most recent row.

    If you only want to view duplicate rows, you can do:

    ; WITH cnts AS (
         SELECT *, cnt = COUNT(*) OVER(PARTITION BY keycol)
         FROM tbl
    )
    SELECT * FROM cnts WHERE cnt > 1
    

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,581 Reputation points
    2024-07-31T05:16:00.2333333+00:00

    And how do you define duplicates?

    Can't be by the primary key, because that key is unique.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.