
결과는 무조건 3개의 연봉 카테고리를 포함해야 한다.
만약 High Salary 수급자가 없으면 0을 반환해야 한다.

with category_in as
(select
*,
case when income < 20000 then 'Low Salary'
when income <= 50000 then 'Average Salary'
when income > 50000 then 'High Salary' end category
from accounts
)
select category, count(account_id) accounts_count
from category_in
group by category

Average Salary category 가 없다!! 분명 문제에서 0이어도 나와야 한다고 했는데...
과연 이 문제는 어떻게 풀어야 할까? 예상외로 아주 무식한 방법으로 풀어야 했다.
(select
"High Salary" as category,
(select count(*) from accounts where income > 50000) as accounts_count)
union
(select
"Average Salary" as category,
(select count(*) from accounts where income >= 20000 and income <= 50000) as accounts_count)
union
(select
"Low Salary" as category,
(select count(*) from accounts where income < 20000) as accounts_count)
select as 를 통해 column 데이터를 행 단위로 하나씩 만들고, 해당 column에 해당하는 row 데이터를 집계함수로 계산해서 넣어준다.