|
| 1 | +907. Count Salary Categories |
| 2 | +Solved |
| 3 | +Medium |
| 4 | +Topics |
| 5 | +Companies |
| 6 | +SQL Schema |
| 7 | +Pandas Schema |
| 8 | +Table: Accounts |
| 9 | + |
| 10 | ++-------------+------+ |
| 11 | +| Column Name | Type | |
| 12 | ++-------------+------+ |
| 13 | +| account_id | int | |
| 14 | +| income | int | |
| 15 | ++-------------+------+ |
| 16 | +account_id is the primary key (column with unique values) for this table. |
| 17 | +Each row contains information about the monthly income for one bank account. |
| 18 | + |
| 19 | + |
| 20 | +Write a solution to calculate the number of bank accounts for each salary category. The salary categories are: |
| 21 | + |
| 22 | +"Low Salary": All the salaries strictly less than $20000. |
| 23 | +"Average Salary": All the salaries in the inclusive range [$20000, $50000]. |
| 24 | +"High Salary": All the salaries strictly greater than $50000. |
| 25 | +The result table must contain all three categories. If there are no accounts in a category, return 0. |
| 26 | + |
| 27 | +Return the result table in any order. |
| 28 | + |
| 29 | +The result format is in the following example. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +Example 1: |
| 34 | + |
| 35 | +Input: |
| 36 | +Accounts table: |
| 37 | ++------------+--------+ |
| 38 | +| account_id | income | |
| 39 | ++------------+--------+ |
| 40 | +| 3 | 108939 | |
| 41 | +| 2 | 12747 | |
| 42 | +| 8 | 87709 | |
| 43 | +| 6 | 91796 | |
| 44 | ++------------+--------+ |
| 45 | +Output: |
| 46 | ++----------------+----------------+ |
| 47 | +| category | accounts_count | |
| 48 | ++----------------+----------------+ |
| 49 | +| Low Salary | 1 | |
| 50 | +| Average Salary | 0 | |
| 51 | +| High Salary | 3 | |
| 52 | ++----------------+----------------+ |
| 53 | +Explanation: |
| 54 | +Low Salary: Account 2. |
| 55 | +Average Salary: No accounts. |
| 56 | +High Salary: Accounts 3, 6, and 8. |
| 57 | + |
| 58 | + |
| 59 | +# Write your MySQL query statement below |
| 60 | +WITH |
| 61 | + S AS ( |
| 62 | + SELECT 'Low Salary' AS category |
| 63 | + UNION |
| 64 | + SELECT 'Average Salary' |
| 65 | + UNION |
| 66 | + SELECT 'High Salary' |
| 67 | + ), |
| 68 | + T AS ( |
| 69 | + SELECT |
| 70 | + CASE |
| 71 | + WHEN income < 20000 THEN "Low Salary" |
| 72 | + WHEN income > 50000 THEN 'High Salary' |
| 73 | + ELSE 'Average Salary' |
| 74 | + END AS category, |
| 75 | + COUNT(1) AS accounts_count |
| 76 | + FROM Accounts |
| 77 | + GROUP BY 1 |
| 78 | + ) |
| 79 | +SELECT category, IFNULL(accounts_count, 0) AS accounts_count |
| 80 | +FROM |
| 81 | + S |
| 82 | + LEFT JOIN T USING (category); |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +### 1. Common Table Expression (CTE) "S" |
| 87 | + |
| 88 | + |
| 89 | +WITH |
| 90 | + S AS ( |
| 91 | + SELECT 'Low Salary' AS category |
| 92 | + UNION |
| 93 | + SELECT 'Average Salary' |
| 94 | + UNION |
| 95 | + SELECT 'High Salary' |
| 96 | + ), |
| 97 | + |
| 98 | + |
| 99 | +- **Purpose:** |
| 100 | + This CTE defines a static list of salary categories. |
| 101 | +- **How it works:** |
| 102 | + - The `SELECT` statements with `UNION` combine three rows, each containing one of the categories: `'Low Salary'`, `'Average Salary'`, and `'High Salary'`. |
| 103 | +- **Result:** |
| 104 | + The result of `S` is a temporary table with one column (`category`) and three rows. |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +### 2. Common Table Expression (CTE) "T" |
| 109 | + |
| 110 | +```sql |
| 111 | + T AS ( |
| 112 | + SELECT |
| 113 | + CASE |
| 114 | + WHEN income < 20000 THEN "Low Salary" |
| 115 | + WHEN income > 50000 THEN 'High Salary' |
| 116 | + ELSE 'Average Salary' |
| 117 | + END AS category, |
| 118 | + COUNT(1) AS accounts_count |
| 119 | + FROM Accounts |
| 120 | + GROUP BY 1 |
| 121 | + ) |
| 122 | +``` |
| 123 | + |
| 124 | +- **Purpose:** |
| 125 | + This CTE categorizes each account from the `Accounts` table based on the `income` value, then counts the number of accounts in each category. |
| 126 | +- **How it works:** |
| 127 | + - **CASE Statement:** |
| 128 | + - If `income` is less than 20000, it labels the row as `"Low Salary"`. |
| 129 | + - If `income` is greater than 50000, it labels the row as `"High Salary"`. |
| 130 | + - Otherwise, it labels the row as `"Average Salary"`. |
| 131 | + - **COUNT(1):** |
| 132 | + - It counts the number of rows (accounts) in each category. |
| 133 | + - **GROUP BY 1:** |
| 134 | + - It groups the results by the first column in the SELECT list, which is the computed `category`. |
| 135 | +- **Result:** |
| 136 | + The result of `T` is a temporary table that contains two columns: `category` and `accounts_count`. It holds the count of accounts for each salary category that exists in the `Accounts` table. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +### 3. Final SELECT with LEFT JOIN |
| 141 | + |
| 142 | +```sql |
| 143 | +SELECT category, IFNULL(accounts_count, 0) AS accounts_count |
| 144 | +FROM |
| 145 | + S |
| 146 | + LEFT JOIN T USING (category); |
| 147 | +``` |
| 148 | + |
| 149 | +- **Purpose:** |
| 150 | + This part combines the two CTEs (`S` and `T`) to ensure that every salary category from `S` is included in the final result, even if there are no corresponding accounts in `T`. |
| 151 | +- **How it works:** |
| 152 | + - **LEFT JOIN:** |
| 153 | + - It joins `S` (all predefined categories) with `T` (the computed counts) on the `category` column. |
| 154 | + - If a category from `S` does not exist in `T` (i.e., there were no accounts that fell into that category), the join will produce a `NULL` value for `accounts_count`. |
| 155 | + - **IFNULL(accounts_count, 0):** |
| 156 | + - This function replaces any `NULL` in `accounts_count` with `0`, ensuring that the final output shows 0 for categories with no accounts. |
| 157 | +- **Result:** |
| 158 | + The final output is a list of salary categories along with the corresponding count of accounts. If a category has no accounts, it will show as 0. |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +### Summary |
| 163 | + |
| 164 | +- **CTE "S":** Defines a static list of salary categories. |
| 165 | +- **CTE "T":** Categorizes and counts accounts from the `Accounts` table based on income. |
| 166 | +- **LEFT JOIN:** Combines both CTEs so every predefined category appears in the final result, with missing counts defaulting to 0. |
| 167 | +- **Final Output:** |
| 168 | + A table with two columns: |
| 169 | + - `category`: The salary category (Low Salary, Average Salary, High Salary). |
| 170 | + - `accounts_count`: The number of accounts in that category (or 0 if there are none). |
0 commit comments