카테고리 없음

[해커랭크] MySQL WHERE절 서브쿼리 - Top Earners

황금붕어빵 2023. 9. 11. 22:11

 

 


 

 

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

이번 문제는 그 동안 급여를 가장 많이 받은 급여의 금액과 몇 명이 받았는지 출력하는 문제입니다.

 

총 급여는 salary x month로 정의하고 (각 직원의 급여와 재직 개월 수) 총 급여를 구합니다.

문제)
We define an employee's total earnings to be their monthly salary x months worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers.

출처 : 해커랭크

salary x month 컬럼을 기준으로 GROUP BY를 진행하면 될 것 같습니다.

 

그리고 WHERE 절을 통해서 가장 큰 총 급여 금액만 출력하면 됩니다.

select months*salary
     , count(*)
from employee
where months*salary = (select max(months*salary) from employee)
group by months*salary

WHERE 절에 서브쿼리 형태로 문제를 풀면됩니다.