6

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.

3
  • You should have checked the existence of the row before adding it again... Can the code column be the same, but with a different user? Commented Jan 7, 2018 at 0:04
  • Roughly to find the duplicate rows the query should be something like: select code, username from listed_users group by code, username having count(1) > 1 Commented Jan 7, 2018 at 0:09
  • @cricket_007 Yeah, a user can have more than one code listed. Commented Jan 7, 2018 at 1:10

1 Answer 1

8

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.