Deleting duplicate rows in sqlite? Deleting duplicate rows in sqlite? sqlite sqlite

Deleting duplicate rows in sqlite?


You can do

DELETE FROM players WHERE id NOT IN(  SELECT MIN(id) id    FROM players   GROUP BY wowp_id, name);

Note: before proceeding with DELETE make sure that you have a solid backup of your data.

After deleting duplicates from your table make sure to create a UNIQUE constraint

CREATE UNIQUE INDEX idx_wowp_id_name ON players(wowp_id, name);

Outcome after deduping:

|  id |   wowp_id |      name | team ||-----|-----------|-----------|------|| 108 | 501078041 | prazluges | None || 109 | 507894244 |     Aidis | None || 110 | 500742127 |     Aidis | None || 111 |     Aidis |     Aidis | None || 113 | 500864543 | prazluges | None || 117 | 501078041 |     satih | None |

Here is SQLFiddle demo


You can define a unique index on the column, or define a unique constraint on the table using indexed columns.

CREATE UNIQUE INDEX IF NOT EXISTS ...

CREATE TABLE IF NOT EXISTS ... (..., UNIQUE(col1, col2, col3), ...)

These help prevent repetition before it arises. Links are to the SQLite documentation.


Import your results into a set of a custom class with the contains method defined to keep what you want. Example follows:

class players:   def __contains__(self, item):        return self.playersObj.name != item.name   # Your other methods go here

Then import your rows into an instance of players and write them back out.