[LeetCode/SQL] 1907. Count Salary Categories

Sooyeon·2024년 1월 16일
0

문제풀이 

목록 보기
92/95
post-thumbnail

[LeetCode/SQL]


📌 1907. Count Salary Categories

문제

풀이

- 급여 범주에 따라 각 범주에 속하는 은행 계좌 수 출력 - "Low Salary," :20000 이하
- "Average Salary," :20000이상 50000 이하
-"High Salary" : 50000 초과 
- 만약 해당 범주에 속하는 계좌가 없으면 0을 반환

방법 I

WITH SalaryCategories AS (
    SELECT
        'Low Salary' AS category,
        COUNT(*) AS accounts_count
    FROM Accounts
    WHERE income < 20000

    UNION ALL

    SELECT
        'Average Salary' AS category,
        COUNT(*) AS accounts_count
    FROM Accounts
    WHERE income BETWEEN 20000 AND 50000

    UNION ALL

    SELECT
        'High Salary' AS category,
        COUNT(*) AS accounts_count
    FROM Accounts
    WHERE income > 50000
)

SELECT
    category,
    COALESCE(accounts_count, 0) AS accounts_count
FROM SalaryCategories;

0개의 댓글