| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 완전탐색
- 코딩테스트
- tableau
- lv4
- Stack
- coding
- 코테
- 조합
- level4
- itertools
- coding test
- LeetCode
- python
- Level2
- lambda
- collections
- counter
- 프로그래머스
- cte
- import re
- time complexity
- AARRR
- programmers
- Queue
- mysql
- join
- sql
- 코딩
- Growth hacking
- 파이썬
Archives
- Today
- Total
ror_coding
[LeetCode] 1907. Count Salary Categories 본문
728x90
출력해야 하는 항목을 고정시킬 때, union all과 select 을 써서 left/right join 을 해야합니다.
Question

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.
The result format is in the following example.

Code
with count_acc as(
select
case
when income < 20000 then 'Low Salary'
when (20000 <= income) and (income <= 50000) then 'Average Salary'
when 50000 < income then 'High Salary'
-- else 'Average Salary'
end as "category"
from accounts
)
select j.category
, count(c.category) as accounts_count
from count_acc c
right join (select 'Low Salary' as category
union all
select 'Average Salary' as category
union all
select 'High Salary' as category
) j on c.category = j.category
group by 1
;
My code
728x90
'Coding Test > SQL' 카테고리의 다른 글
| [LeetCode] 1341. Movie Rating (0) | 2025.07.19 |
|---|---|
| [LeetCode] 626. Exchange Seats (0) | 2025.07.19 |
| [LeetCode] 1934. Confirmation Rate (0) | 2025.07.13 |
| [LeetCode] 176. Second Highest Salary (0) | 2025.07.13 |
| [HackerRank] Top Earners (0) | 2025.06.14 |