[코드카타] SQL 51 Count Salary Categories

Data_Student·2024년 11월 27일
0

코드카타

목록 보기
60/82

[코드카타] SQL 51 Count Salary Categories

51. Count Salary Categories
https://leetcode.com/problems/count-salary-categories/

Write a solution to calculate the number of bank accounts 
for each salary category. The salary categories are:
"Low Salary": All the salaries strictly less than $20000.
"Average Salary": All the salaries in the inclusive range [$20000, $50000].
"High Salary": All the salaries strictly greater than $50000.
The result table must contain all three categories. 
If there are no accounts in a category, return 0.
Return the result table in any order.
with category as (
    select 'Low Salary' category
    union all
    select 'Average Salary' category
    union all
    select 'High Salary' category
), salary as (
select case when income < 20000 then 'Low Salary'
            when income <= 50000 then 'Average Salary'
            else 'High Salary' end as category
            from Accounts )
select c.category, ifnull(count(s.category),0) accounts_count
from category c left join salary s on c.category = s.category
group by c.category
order by 2 desc
더미 데이터를 사용하여 모든 Row가 반드시 나타나게 하기! ★
count() 함수 내에 작성하는 컬럼에 주의하기!
'*'을 사용할 경우 null도 1로 카운트함!

0개의 댓글