Challenges

  • + 13 comments

    It took me an hour to correctly write the query. For people struggling with the logic I advice you to take a paper and pen and analyze how you can seperate the data which is required.

        select a.hacker_id,a.name,count(b.hacker_id)    
        from Hackers a, Challenges b
        WHERE a.hacker_id = b.hacker_id
        GROUP BY a.hacker_id,a.name
        HAVING count(b.hacker_id) not in (select  distinct          count(hacker_id) from Challenges
        WHERE hacker_id <> a.hacker_id
        group by hacker_id
        having count(hacker_id) < (select                           max(x.challenge_count) 
        from (select count(b.challenge_id) as challenge_count                 from Challenges b GROUP BY b.hacker_id) as x ))
        ORDER BY count(b.hacker_id) desc, a.hacker_id