Prepare > SQL > Aggregation > Top Earners
2023. 5. 17. 20:24ㆍHackerRank-My SQL
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.
=> 누적수령액 최고값과 수령액이 최대인 사람 수를 쿼리하라.
코드
SELECT MAX(salary * months), COUNT(*) FROM EMPLOYEE
WHERE salary * months =(SELECT MAX(salary * months) FROM EMPLOYEE);
- 누적수령액을 한 번만 쿼리
노트
GROUP BY 이용
SELECT salary * months, COUNT(*) FROM EMPLOYEE
GROUP BY salary * months
ORDER BY salary * months DESC
LIMIT 0 ,1;
- LIMIT 1, 1 는 1번 인덱스(두 번째 행)부터 1개 보여주는 것: 0부터 시작임
- 한 줄 한 줄 실행해보며 코드 작성
두 번째 시도
SELECT MAX(salary * months), COUNT(*) FROM EMPLOYEE
WHERE salary * months = (SELECT MAX(salary * months) FROM EMPLOYEE);