I found this solution online to a HackerRank problem:
SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS cnt
FROM Hackers AS h
JOIN Challenges AS c ON h.hacker_id = c.hacker_id
GROUP BY c.hacker_id, h.name
HAVING
cnt = (SELECT COUNT(c1.challenge_id) FROM Challenges AS c1 GROUP BY c1.hacker_id ORDER BY COUNT(*) DESC LIMIT 1) OR
cnt NOT IN (SELECT COUNT(c2.challenge_id) FROM Challenges AS c2 GROUP BY c2.hacker_id HAVING c2.hacker_id <> c.hacker_id)
ORDER BY cnt DESC, c.hacker_id;
As you can see, cnt
is defined in SELECT, and then used in HAVING. My understanding was that, logically speaking, GROUP BY and HAVING run before SELECT. Therefore I would expect that SQL would not recognize cnt
when executing GROUP BY, and throw an error. However, the code gives the correct solution.
Can anyone explain why? Maybe MySQL corrects for this artificially?