Query to find Maximum friends not giving correct resuts Table Schema for the Likes table:

id id1
1  2
1  3
2  1


I count the total number of Likes for each student with this:

select id,count(*)
from friends
group by id


I have to find the students with the maximum number of Likes, so I use this query as a temp table:

select * from (
  select f1.id,count(*) as count1
  from Likes f1
  group by id
) temp
where not exists (
  select f2.id,count(asterick) as count2
  from Likes f2 group by f2.id having count2 &gt; temp.count1
)


It returns all of the records. Can anyone point out what I am doing wrong in this query?
Your query is correct and works fine in other DBMS (SQL-Server, Postgres, Oracle, MySQL). In SQLite, it appears there is a bug, possibly due to the correlated subquery and/or the grouping in both the main and the sub query. Here is another way to write the query:

SELECT id, COUNT(*) AS count1
FROM Likes
GROUP BY id
HAVING COUNT(*) = 
      ( SELECT COUNT(*) AS cnt
        FROM Likes
        GROUP BY id
        ORDER BY cnt DESC
          LIMIT 1
      ) ;


Tested in SQL-Fiddle