Milky's note

[HackerRank](E) Top Earners 본문

SQL/SQL 코딩 테스트-HackerRank

[HackerRank](E) Top Earners

밀뿌 2022. 3. 25. 17:26

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

Input Format

The Employee table containing employee data for a company is described as follows:

where employee_id is an employee's ID number, name is their name, months is the total number of months they've been working for the company, and salary is the their monthly salary.

 

 

[답]

-mysql

select (months*salary) as earnings,
count(*)
from Employee
group by earnings
order by earnings desc
limit 1

이것 역시 문제는 길지만 최고로 연봉이 높은 금액과 그 금액을 받는 사람이 몇 명인지 구하는 문제이다. 

먼저 주어지지 않은 earnings 를 구하기 위해 months와 salary를 곱해주어 새로운 컬럼을 만들어준다.

그 뒤에 그 컬럼으로 그룹화와 정렬을 해서, 제일 높은 첫번째 연봉만 구해준다.

count(*)를 해주면 어차피 limit에서 제일 높은 연봉만 걸러지기 때문에 전체 count를 하면 완료가 된다.

Comments