SQL 문제풀이

[해커랭크 SQL] Aggregation - Top Earners

냄비짱 2023. 8. 25. 16:58
728x90

❓ Question
https://www.hackerrank.com/challenges/earnings-of-employees

 

Top Earners | HackerRank

Find the maximum amount of money earned by any employee, as well as the number of top earners (people who have earned this amount).

www.hackerrank.com

 

 

❗ Answer

SELECT months*salary, COUNT(*)
    FROM employee
    WHERE months*salary = (SELECT max(months*salary)
                            FROM employee)
    GROUP BY months*salary;

📌 Discussion

  • sub query scalar를 활용하여 최대의 총 급여를 구한다.
  • 이후 where 절에서 총 급여가 최대값과 같은 레코드를 추출한다.
  • 이후 총 급여로 group by 후 count 집계를 해준다.