I am doing a practice SQL questions Hacker Rank. here is the website https://www.hackerrank.com/challenges/challenges/problem
.
So, If put the following code, it will give me right answer:
select h.hacker_id,h.name,count(c.challenge_id) num
from Hackers h, Challenges c
where h.hacker_id = c.hacker_id
group by h.hacker_id, h.name
having count(c.challenge_id) in
(8 ,9 ,10 ,14 ,15 ,16 ,18 ,19 ,20 ,21 ,22 ,23 ,24 ,25 ,
26 ,27 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,35 ,36 ,37 ,40 ,41 ,42 ,50)
order by num desc, h.hacker_id
But the number
(8 ,9 ,10 ,14 ,15 ,16 ,18 ,19 ,20 ,21 ,22 ,23 ,24 ,25 ,
26 ,27 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,35 ,36 ,37 ,40 ,41 ,42 ,50)
is actually generate by this following code:
select t1.num2 from
(select hacker_id id_1, count(challenge_id) num2 from challenges
group by hacker_id
order by num2 desc) t1
group by t1.num2
having count(t1.num2)=1 or count(t1.num2) = 12
If you replace the number with the code above as a subquery, like:
select h.hacker_id,h.name,count(c.challenge_id) num
from Hackers h, Challenges c
where h.hacker_id = c.hacker_id
group by h.hacker_id, h.name
having count(c.challenge_id) in
(select t1.num2 from
(select hacker_id id_1, count(challenge_id) num2 from challenges
group by hacker_id
order by num2 desc) t1
group by t1.num2
having count(t1.num2)=1 or count(t1.num2) = 12)
It simply will not return any values. Could anyone tell me why is this?