Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Table Schema

Likes table
id,id1
1,2
1,3
2,1

I have count of total number of Likes for each student through this


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

Now I have to find students with maximum Likes, so I use this query as 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 > temp.count1)

It returns all of the records. Can anyone point out what I am doing wrong in this query.

share|improve this question
Your query seems correct (although it could be simplified). I guess all your users have the same amount of friends. – ypercube Feb 24 at 21:48

1 Answer

If I understand your requirements correctly this is a simplified query to get your results, although when I tested yours it worked fine as well. The simplified one may point out a way to get the correct results in your real query with any luck.

WITH Totals AS (SELECT id, COUNT(1) AS count1 
                FROM Likes GROUP BY id)
SELECT *
FROM Totals f1
WHERE count1 = (SELECT MAX(count1) FROM Totals)
share|improve this answer
1  
The query seems correct but have you tested it in SQLite? I don't think sqlite has CTEs. – ypercube Feb 25 at 9:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.