본문 바로가기
데이터 분석/SQL 연습문제

Top Earners (HackerRank)

2022. 9. 17.
 

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 salary * months as earnings, count(*)
from employee
where salary * months = (select max(salary * months) from employee)
group by earnings

-> where 절 서브쿼리 사용(집계함수 max 값이 하나이므로)

다른 답

select (months * salary) earnings, count(employee_id)
from employee
group by earnings
having earnings = (select max(months * salary) from employee)

-> having 절 사용하여 조건 부여

2022.09.17

댓글