본문 바로가기

For workingHoliday/SQL

HackerRank SQL

 

 

 

To summarize question :  just get the max total earning from one table, Employee, and count it how many employees get the hight salary.

 

 

for soving this problem, I need to conside how can I get the max earnings and how can I count the all of employees who get the most earnings.

so, I choose to use aggregation method, SUM and COUNT.

It was really easy to get max earnings but for counting the employees, it was quiet difficult to me.

so I tried to use sub query. because jsut use COUNT() in this problem, it came up with wrong answer, like 106.

it maybe get total of employess, so I don't have choice, just use subquery. 

sub query is good, because it can derive answer more simply I think.

and I think the sql is more intuitive.

so My answer is that.

 

 

why I use sub query in WHERE? 

because I need to count all of employees who get most high salary. so If the sub query use in WHERE, the answer can be made according condition. 

 

Also, I was qurious about other people's answer, so I searched the answer, and I picked one between them.

because it was quiet similar with my origin answer. 

I tried to solve this problem like that. but I didn't know that can use operator in ORDER BY section.

so I can learn one thing from this sql.

 

OTHER'S ANSWER :

SELECT salary*months AS P, COUNT(*) FROM Employee GROUP BY(salary*months) ORDER BY (salary*months) DESC LIMIT 1;

'For workingHoliday > SQL' 카테고리의 다른 글

Hacker Rank Join and aggregation methon  (0) 2024.03.01