Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Query to delete and display duplicate records

One of the question asked in an interview was,

One table has 100 records. 50 of them are duplicates. Is it possible with a single query to delete the duplicate records from the table as well as select and display the remaining 50 records.

Is this possible in a single SQL query?

Thanks

SNA

like image 213
SNA Avatar asked Jan 28 '10 07:01

SNA


2 Answers

with SQL Server you would use something like this

DECLARE @Table TABLE (ID INTEGER, PossibleDuplicate INTEGER)

INSERT INTO @Table VALUES (1, 100)
INSERT INTO @Table VALUES (2, 100)
INSERT INTO @Table VALUES (3, 200)
INSERT INTO @Table VALUES (4, 200)

DELETE FROM @Table
OUTPUT Deleted.*
FROM  @Table t
      INNER JOIN (
        SELECT    ID = MAX(ID)
        FROM      @Table
        GROUP BY  PossibleDuplicate
        HAVING    COUNT(*) > 1
      ) d ON d.ID = t.ID

The OUTPUT statement shows the records that get deleted.

Update:

Above query will delete duplicates and give you the rows that are deleted, not the rows that remain. If that is important to you (all in all, the remaining 50 rows should be identical to the 50 deleted rows), you could use SQL Server's 2008 MERGE syntax to achieve this.

like image 167
Lieven Keersmaekers Avatar answered Sep 21 '22 21:09

Lieven Keersmaekers


Lieven's Answer is a good explanation of how to output the deleted rows. I'd like to add two things:

  1. If you want to do something more with the output other than displaying it, you can specify OUTPUT INTO @Tbl (where @Tbl is a table-var you declare before the deleted);

  2. Using MAX, MIN, or any of the other aggregates can only handle one duplicate row per group. If it's possible for you to have many duplicates, the following SQL Server 2005+ code will help do that:

 

;WITH Duplicates AS
(
    SELECT
        ID,
        ROW_NUMBER() OVER (PARTITION BY DupeColumn ORDER BY ID) AS RowNum
)
DELETE FROM MyTable
OUTPUT deleted.*
WHERE ID IN
(
    SELECT ID
    FROM Duplicates
    WHERE RowNum > 1
)
like image 44
Aaronaught Avatar answered Sep 21 '22 21:09

Aaronaught