상세 컨텐츠

본문 제목

Top Earners(HackerRank)

SQL/MySQL 문제풀이

by 관재탑 2022. 7. 18. 18:53

본문

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  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.

 

 

정답쿼리

SELECT MAX(salary*months), COUNT(employee_id) 
FROM Employee
WHERE salary*months = 108064

 

해설

문제에서 직원들이 받는 가장 높은 연봉의 액수와 가장 높은 연봉을 받는 직원의 수를 구하라했으므로

1. SELECT MAX(salary*months)로 가장 높은 연봉의 액수를 구하고

2. WHERE salary*months = 108064로 가장 높은 연봉을 받는 직원들로만 필터링 한뒤에

3. COUNT(employee_id)로 가장 높은 연봉을 받는 직원들이 수를 구해준다.

 

이렇게 GROUP BY를 이용해서도 풀 수 있다.

SELECT salary*months AS earnings, COUNT(*)
FROM Employee
GROUP BY earnings
ORDER BY earnings DESC
LIMIT 1

관련글 더보기

댓글 영역