Question
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 2 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.
Sample Input
Sample Output
69952 1
Explanation
The table and earnings data is depicted in the following diagram:
The maximum earnings value is 69952. The only employee with earnings = 69952 is Kimberly, so we print the maximum earnings value (69952) and a count of the number of employees who have earned $69952 (which is 1) as two space-separated values.
Summary
Salay * Months 가 가장 큰 금액을 파악하고, 가장 큰 금액을 받은 인원 수를 파악하라
Solve
select salary * months earnings
, count(*)
from employee
group by earnings
order by earnings desc
limit 1
- salary * months 로 earnings를 계산, earning 별로 인원수를 파악한다.
- earning을 오름차순으로 정렬, 첫번째 행만(가장 큰 금액) 출력한다.
Issue
쉬운 난이도인 해당 문제를 블로그에 쓰게 된 이유이자 이슈 : '가장 큰 금액' 에 꽂혀서 max()함수로만 접근했다.
그러다보니 한 쿼리 안에 두개에 함수를 적으려고 하거나, 이중 쿼리를 작성하며 이상한 문법을 구사하는 필자를 발견.
처음부터 문제를 다시 보고 풀게 되었다.
어느 부분에서 집계가 이루어지는지 우선순위를 파악하고 문제에 접근하자고 다짐하게 된 쿼리였다.
Source
https://www.hackerrank.com/challenges/earnings-of-employees/problem?isFullScreen=true
'Study > SQL' 카테고리의 다른 글
MySQL | Temporary Table (0) | 2023.02.09 |
---|---|
MySQL | HackerRank | Ollivander's Inventory (0) | 2023.02.07 |
MySQL | HackerRank | Top Competitors (0) | 2023.01.31 |
MySQL | HackerRank | The Report (0) | 2023.01.30 |
MySQL | HackerRank | The PADS (0) | 2023.01.27 |