Skip to content

Instantly share code, notes, and snippets.

@GGrassiant
Last active March 24, 2021 12:57
Show Gist options
  • Select an option

  • Save GGrassiant/235e7083004f04c5a9abecaf36854c8b to your computer and use it in GitHub Desktop.

Select an option

Save GGrassiant/235e7083004f04c5a9abecaf36854c8b to your computer and use it in GitHub Desktop.
Complex SQL Query
/* these are the columns we want to output */
select c.hacker_id, h.name ,count(c.hacker_id) as c_count
/* this is the join we want to output them from */
from Hackers as h
inner join Challenges as c on c.hacker_id = h.hacker_id
/* after they have been grouped by hacker */
group by c.hacker_id, h.name
/* but we want to be selective about which hackers we output */
/* having is required (instead of where) for filtering on groups */
having
/* output anyone with a count that is equal to... */
c_count =
/* the max count that anyone has */
(SELECT MAX(temp1.cnt)
from (SELECT COUNT(hacker_id) as cnt
from Challenges
group by hacker_id
order by hacker_id) temp1)
/* or anyone whose count is in... */
or c_count in
/* the set of counts... */
(select t.cnt
from (select count(*) as cnt
from challenges
group by hacker_id) t
/* whose group of counts... */
group by t.cnt
/* has only one element */
having count(t.cnt) = 1)
/* finally, the order the rows should be output */
order by c_count DESC, c.hacker_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment