본문 바로가기
HackerRank

해커랭크 Top Earners

by 콩순이냉장고 2022. 12. 15.

문제 URL : 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

문제 설명 : 소득이 가장 많은 금액을 구하라는겁니다 단, 중복된것이 몇개나 있는지 출력하는 문제입니다.

소득(earning) = salary * months 이걸로 구해야겠죠

 

2가지 답이있는데 처음풀었을때와

2번째로 풀었을때 두가지 드릴테니 잘비교해서 상황에따라 어떻게 해석할수있을지 잘생각해보세요

 

첫번째 sql

select months * salary ,count(*) 
from employee
group by months * salary
order by months * salary desc
limit 1

 

두번째 sql 

select b.earn,count(*)
from employee a
inner join (select salary * months as earn from employee order by salary * months desc limit 1) as b
on a.salary * a.months = b.earn
group by b.earn

 

첫번째는 sql 계한한결과들은 그룹으로 묶어서 정렬하여 하나만 출력한것이고

두번째는 가장큰값 한개를 찾아서 셀프조인해서 그것을 group by로 하나로 출력한것입니다.

첫번째가 가장 코드는 깔끔한데 코드를 짜는 사람의 생각은 다 다다르니

 

전 이문제를 풀때 첫번째가 생각이안나서 두번째 방법으로 풀었었네요

'HackerRank' 카테고리의 다른 글

해커랭크 The PADS  (0) 2022.12.15
해커랭크 Weather Observation Station 5 ->Mysql  (0) 2022.11.30
해커랭크 Weather Observation Station 6 ->MySql  (0) 2022.11.30