|
81 | 81 | S |
82 | 82 | LEFT JOIN T USING (category); |
83 | 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