ror_coding

[HackerRank] Top Earners 본문

Coding Test/SQL

[HackerRank] Top Earners

ro_rdil_31 2025. 6. 14. 15:47
728x90

MySQL에선 select 구문의 alias를 group by 에서 쓸 수 있다 !

 

Question

 

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.

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: 

 

Code (After 250821)

 

select max(earnings), count(*) as cnt
from (select *, months * salary as earnings from employee) s
where earnings = (select max(months * salary) from employee)

 

Code (Before)

 

-- where 절 서브쿼리
SELECT months * salary, count(*)
FROM employee
where months * salary = (SELECT max(months * salary) FROM employee)
group by 1

-- having 절 서브쿼리
select months * salary as earnings, count(*)
from employee
group by earnings -- MySQL에선 select 구문 alias를 여기서 쓸 수 있다 !
having earnings = (SELECT max(months * salary) FROM employee)

 

now me

728x90

'Coding Test > SQL' 카테고리의 다른 글

[LeetCode] 1934. Confirmation Rate  (0) 2025.07.13
[LeetCode] 176. Second Highest Salary  (0) 2025.07.13
[HackerRank] Type of Triangle  (0) 2025.06.14
[HackerRank] Symmetric Pairs  (0) 2025.06.14
[HackerRank] Binary Tree Nodes  (0) 2025.04.05