inblog logo
|
silver
    SQL문제풀기

    [SQL문제풀기] Count Salary Categories

    silver's avatar
    silver
    Nov 12, 2025
    [SQL문제풀기] Count Salary Categories
    Contents
    문제MySQL

    문제

    Count Salary Categories - LeetCode
    Can you solve this real interview question? Count Salary Categories - Table: Accounts +-------------+------+ | Column Name | Type | +-------------+------+ | account_id | int | | income | int | +-------------+------+ account_id is the primary key (column with unique values) for this table. Each row contains information about the monthly income for one bank account.   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.   Example 1: Input: Accounts table: +------------+--------+ | account_id | income | +------------+--------+ | 3 | 108939 | | 2 | 12747 | | 8 | 87709 | | 6 | 91796 | +------------+--------+ Output: +----------------+----------------+ | category | accounts_count | +----------------+----------------+ | Low Salary | 1 | | Average Salary | 0 | | High Salary | 3 | +----------------+----------------+ Explanation: Low Salary: Account 2. Average Salary: No accounts. High Salary: Accounts 3, 6, and 8.
    Count Salary Categories - LeetCode
    https://leetcode.com/problems/count-salary-categories/?envType=study-plan-v2&envId=top-sql-50
    Count Salary Categories - LeetCode

    MySQL

    내가 작성한 정답

    with c as (select case when income < 20000 then 'Low Salary' when income > 50000 then 'High Salary' else 'Average Salary' end category ,account_id from Accounts) select d.category, count(c.account_id) accounts_count from c c right join (select 'Low Salary' category union all select 'High Salary' union all select 'Average Salary') d on c.category = d.category group by category;
    Share article

    silver

    RSS·Powered by Inblog