Check Subset

  • + 2 comments

    maybe not shortest one but I quess clear one

    /*
    Enter your query here.
    Please append a semicolon ";" at the end of the query and enter your query in a single line to avoid error.
    */
    with ranking as
    (
    select 
      h.hacker_id,
      h.name,
      count(c.challenge_id) counter
    from
      hackers h
      join challenges c on h.hacker_id = c.hacker_id
    group by 
      h.hacker_id,
      h.name
    )
    , maximum as
    (
    select 
      max(counter) max_counter
    from ranking
    )
    , quantity as
    (
    select 
      counter,
      count(counter) count_counter
    from ranking
    group by counter
    )
    
    select 
      r.* 
    from
      ranking r
      join maximum m on r.counter <= m.max_counter
      join quantity q on r.counter = q.counter
    where
      q.count_counter = 1 or r.counter = m.max_counter
    order by
      r.counter desc,
      r.hacker_id asc