본문 바로가기

SQL/StrataScratch

[MySQL] (Medium) Workers With The Highest Salaries

문제: https://platform.stratascratch.com/coding/10353-workers-with-the-highest-salaries?code_type=3

 

나의 풀이:

select worker_title AS best_paid_title 
from (
    select worker_title 
         , dense_rank() over (order by salary desc) AS ranking
    from worker w
         join title t on w.worker_id = t.worker_ref_id) sub
where ranking = 1;

 

어제 랭크 덴스 랭크 문제 미세한 차이를 주의해야겠다 했는데

바로 오늘 썼다

근데 이번 문제에서는 명확하게 명시를 해줘서 좋았음!

Your output should include the highest-paid title or multiple titles with the same salary. 이렇게 확실하게 말해줘서

덴스랭크 쓰면 되겠군~ 했다

 

솔루션

SELECT *
FROM
  (SELECT CASE
              WHEN salary =
                     (SELECT max(salary)
                      FROM worker) THEN worker_title
          END AS best_paid_title
   FROM worker a
   INNER JOIN title b ON b.worker_ref_id=a.worker_id
   ORDER BY best_paid_title) sq
WHERE best_paid_title IS NOT NULL;

 

솔루션은 케이스를 썼고 맥스를 썼다

케이스에서 엘스 처리를 안 해줘서 웨어에서 널 처리를 해준듯,,

 

다른 사람이 푼 풀이

select t.worker_title
from worker w 
	 join title t on w.worker_id = t.worker_ref_id 
where salary in (select max(salary)
                    from worker);

이게 제일 간단하고 좋은듯!

예전엔 나도 이렇게 접근했었는데,, (일부러는 아니고 윈도우 함수 쓸 줄 몰라서ㅋㅋ)

이게 참 윈도우 함수 몇번 쓰면 다음 문제도 윈도우 함수 부터 생각나고 그르네

'SQL > StrataScratch' 카테고리의 다른 글

[MySQL] (Medium) New Products  (1) 2024.02.08
[MySQL] (Medium) Finding User Purchases  (2) 2024.02.07
[MySQL] Activity Rank  (0) 2024.02.01
[MySQL] (Medium) Users By Average Session Time  (2) 2024.01.31
[MySQL] (Medium) Most Profitable Companies  (0) 2024.01.26