https://www.hackerrank.com/challenges/earnings-of-employees/problem?h_r=internal-search 

 

 

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  space-separated integers.

 

 

 

문제 해석

① Salary * Months의 값을 earnings라 정의할 것 (Alias)

② 모든 직원 중의 maximum earnings 찾고, maximum earnings을 보유하고 있는 직원의 수 찾기 (GROUP BY, ORDER BY, LIMIT)

 

 

 

Query

SELECT salary * months as earnings
	, COUNT(*)
FROM employee
GROUP BY earnings -- earnings끼리 그룹핑하기
ORDER BY earnings DESC -- earnings가 높은 순서대로 정렬하기
LIMIT 1; -- 가장 높은 값만 추출하기

 

 

주의할 점

하나의 SELECT 절에 Alias를 먼저 쓰고, 바로 뒤에 해당 Alias를 언급하면 에러가 난다. 같은 SELECT 절에서는 동시에 연산이 이루어지기 때문에, 먼저 작성한 Alias는 정의되지 않은 상태이다.

+ Recent posts