기존학습자료/leetcode, hackerrank

해커랭크 - Top earners

gooreumsea 2023. 6. 25. 03:34

Top earners - 서브쿼리
https://www.hackerrank.com/challenges/earnings-of-employees/problem

SELECT months*salary as earnings
     , count(*)
FROM employee

WHERE months*salary = (SELECT MAX(months*salary) FROM employee)
GROUP BY earnings


-- Having >>> GROUP BY 결과물을 다시 한 번 필터링 해줄 때


SELECT months*salary AS earnings
     , count(*)
FROM employee
GROUP BY earnings
HAVING earnings = (SELECT MAX(months*salary) FROM employee)  -- 전체 임직원 중에서 최대 값을 가진 사람만



-- FROM >>> FROM 절을 테이블로 활용
 
SELECT *
FROM (

        SELECT months*salary AS earnings
             , count(*)
        FROM employee
        GROUP BY earnings
    ) sub
ORDER BY earnings DESC
limit 1;