Im between cherry

Hackerrank | MySQL | Top Earners 본문

데이터분석/practice_query

Hackerrank | MySQL | Top Earners

meal 2020. 8. 29. 11:05

Top Earners

Problem

 

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

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.

 

-- Having절 서브쿼리를 이용한 풀이
SELECT months * salary AS earnings
    , COUNT(*)
FROM employee
GROUP BY earnings
HAVING earnings = (SELECT MAX(months*salary) FROM employee)
-- WHERE절 서브쿼리를 이용한 풀이
SELECT months * salary AS earnings
        , COUNT(*)
FROM employee
--highest earnings
WHERE months * salary = (SELECT MAX(months*salary) FROM employee)
GROUP BY earnings

 

Comments