Search code examples
javaandroidsqliteandroid-sqlite

How to query the most duplicate data column in SQLite Android


I'm having a problem querying a SQLite database in Android. The way I do it now is only ORDER BY, but it doesn't work, only the sorting function.

Cursor cursor = DB.rawQuery("SELECT * FROM Userdetails ORDER BY ID DESC", null);

enter image description here

How to query all the most similar values ​​in the ID column? In this case 1ABC occurs at most so it should appear twenty-two times.


Solution

  • If you don't care about ties in the number of duplicates then use a subquery in the WHERE clause:

    SELECT * 
    FROM Userdetails 
    WHERE ID = (SELECT ID FROM Userdetails GROUP BY ID ORDER BY COUNT(*) DESC LIMIT 1);