SQL/HackerRank(MYSQL)

[SQL][HackerRank] Top Earners

삐야오 2022. 4. 18. 14:55

문제 링크

https://www.hackerrank.com/challenges/earnings-of-employees/problem?isFullScreen=true 

 

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

문제

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.

 

 

직원의 총 수입은 월 급여 x (근무한)개월로 정의되며, 최대 총 수입은 Employee 테이블에 있는 모든 직원의 최대 총 수입으로 정의한다. 모든 직원의 최대 총 수입과 최대 총 수입을 가진 직원 수를 찾는 쿼리를 작성하시오. 그 다음, 이 값을 공백으로 구분된 2개의 정수로 인쇄하시오.

 

문제풀이

  • 월 급여(SALARY) * 개월(MONTHS)를 EARNINGS로 정의한다.
  • EARNINGS로 GROUP BY하고 내림차순으로 정렬한다.
  • LIMIT 1을 통해 가장 위에 있는 행(최대 총 수입)과 최대 총 수입을 가진 직원수를 COUNT로 세준다.

 

쿼리문

SELECT SALARY * MONTHS AS EARNINGS, COUNT(*)
FROM EMPLOYEE
GROUP BY EARNINGS
ORDER BY EARNINGS DESC
LIMIT 1;