Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying duplicates rows in an SQLite3 database and deleting them

I have a table listed_users that has two TEXT columns, code and username:

code | username
-----|---------
aa   | john_doe
ab   | jane_doe
ca   | john_doe
ca   | john_doe <-- duplicate
da   | ryan_doe

I'd like to write a command that will delete duplicates such as ca | john_doe, where the same information appears in both columns in more than one row.

like image 292
John Smith Avatar asked Jan 06 '18 23:01

John Smith


People also ask

How do I find duplicates and deletes in SQL?

SQL Delete Duplicate Rows using Group By and Having Clause According to Delete Duplicate Rows in SQL, for finding duplicate rows, you need to use the SQL GROUP BY clause. The COUNT function can be used to verify the occurrence of a row using the Group by clause, which groups data according to the given columns.

How do I delete duplicates in SQLite?

The DISTINCT clause allows you to remove the duplicate rows in the result set. In this syntax: First, the DISTINCT clause must appear immediately after the SELECT keyword. Second, you place a column or a list of columns after the DISTINCT keyword.


1 Answers

To delete one of a pair of duplicate rows, you must have some mechanism to identify it. In SQLite, this would be the rowid.

The following query returns the rowid values of all the rows you want to keep, i.e., one row for each unique code/name combination:

SELECT min(rowid)
FROM listed_users
GROUP BY code, username;

You want to delete all rows not in that list:

DELETE FROM listed_users
WHERE rowid NOT IN (SELECT min(rowid)
                    FROM listed_users
                    GROUP BY code, username);
like image 152
CL. Avatar answered Oct 17 '22 07:10

CL.