SQL/LeetCode
[SQL] Count Salary Categories(LeetCode/Oracle)
DBwiz
2023. 10. 29. 14:59
안녕하세요!
이번 포스팅은 LeetCode에 있는 Count Salary Categories 문제를 OracleDB로 풀어보려고 합니다!
(모든 문제는 Oracle로 풀이하겠습니다.)
1. 문제 링크 : https://leetcode.com/problems/count-salary-categories/description/
2. 문제
| Column name | Type |
| account_id | int |
| income | int |
[문제] 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.
급여 항목별 은행 계좌 수를 계산하는 문제입니다. 급여 항목은 다음과 같습니다:
"낮은 월급": 모든 월급은 엄밀하게 $200,000 미만입니다.
"평균 급여": 포함 범위에 있는 모든 급여[$200,000, $50000].
"높은 월급": 엄밀하게 5만 달러 이상의 월급.
3. 제출 쿼리 및 설명
1) 낮은 월급(Low Salary)의 계좌 수를 조회합니다.
SELECT
'Low Salary' AS CATEGORY,
COUNT(income) AS ACCOUNTS_COUNT
FROM ACCOUNTS
WHERE INCOME < 20000
2) 평균 급여(Average Salary)의 계좌 수를 조회합니다.
SELECT
'Average Salary' AS CATEGORY,
COUNT(income) AS ACCOUNTS_COUNT
FROM ACCOUNTS
WHERE INCOME >= 20000 AND INCOME <= 50000
3) 높은 월급(High Salary)의 계좌 수를 조회합니다.
SELECT
'High Salary' AS CATEGORY,
COUNT(income) AS ACCOUNTS_COUNT
FROM ACCOUNTS
WHERE INCOME > 50000
최종) 1,2,3번 쿼리를 UNION을 활용해서 합칩니다.
SELECT
'Low Salary' AS CATEGORY,
COUNT(income) AS ACCOUNTS_COUNT
FROM ACCOUNTS
WHERE INCOME < 20000
UNION
SELECT
'Average Salary' AS CATEGORY,
COUNT(income) AS ACCOUNTS_COUNT
FROM ACCOUNTS
WHERE INCOME >= 20000 AND INCOME <= 50000
UNION
SELECT
'High Salary' AS CATEGORY,
COUNT(income) AS ACCOUNTS_COUNT
FROM ACCOUNTS
WHERE INCOME > 50000
;