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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With