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.

SQL Server Training
SQL Server Training
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Training: Instruction to develop new skills.
14 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 114.6K Reputation points MVP
    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 45,376 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 Answers by the question author, which helps users to know the answer solved the author's problem.