데이터 분석/SQL 연습문제
Top Earners (HackerRank)
중급닌자 연습생
2022. 9. 17. 20:00
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 절 사용하여 조건 부여