๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/SQL test

[HackerRank] Top Earners ํ’€์ด (mySQL)

by ISLA! 2023. 12. 23.

๋ฌธ์ œ

 

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

 


 

ํ’€์ด

 

  • ๋จผ์ € ์ด๊ธ‰์—ฌ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค : SELECT ์ ˆ์— months * salary๋ฅผ ๊ณฑํ•˜์—ฌ earnings๋ฅผ ๋งŒ๋“ ๋‹ค.
  • ์›ํ•˜๋Š” ๊ฐ’์€ earnings์˜ ์ตœ๋Œ“๊ฐ’์ด๋ฏ€๋กœ, order by earnings desc๋ฅผ ๊ณ ๋ คํ•œ๋‹ค.
  • ์ด๋•Œ, ์ตœ๋Œ€ ์ด ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์ง์› ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด group by earnings๋ฅผ ํ•œ ๋‹ค์Œ, count() ํ•ด์ค€๋‹ค.
SELECT MONTHS * SALARY as earnings, COUNT(*)
FROM EMPLOYEE
GROUP BY earnings
ORDER BY earnings DESC
LIMIT 1;

 

 

 

728x90