Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b30700

Browse files
Update readme.md
1 parent a9bf21a commit 0b30700

File tree

1 file changed

+197
-0
lines changed
  • LeetCode SQL 50 Solution/1907. Count Salary Categories

1 file changed

+197
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
Below is a well-structured `README.md` for **LeetCode 907 - Count Salary Categories**. It includes detailed explanations for the SQL solution using CTEs and a corresponding Pandas solution.
2+
3+
```md
4+
# 💰 Count Salary Categories - LeetCode 907
5+
6+
## 📌 Problem Statement
7+
You are given a table **Accounts** that contains information about bank accounts, including their monthly income.
8+
Your task is to calculate the number of bank accounts in each salary category.
9+
10+
The salary categories are defined as follows:
11+
- **"Low Salary"**: Salaries strictly less than \20,000ドル.
12+
- **"Average Salary"**: Salaries in the inclusive range [\20,000,ドル \50,000ドル].
13+
- **"High Salary"**: Salaries strictly greater than \50,000ドル.
14+
15+
The result table must contain **all three categories**. If there are no accounts in a category, return 0.
16+
17+
Return the result in **any order**.
18+
19+
---
20+
21+
## 📊 Table Structure
22+
23+
### **Accounts Table**
24+
| Column Name | Type |
25+
| ----------- | ---- |
26+
| account_id | int |
27+
| income | int |
28+
29+
- `account_id` is the **primary key** for this table.
30+
- Each row contains the monthly income for one bank account.
31+
32+
---
33+
34+
## 📊 Example 1:
35+
36+
### **Input:**
37+
#### **Accounts Table**
38+
| account_id | income |
39+
| ---------- | ------ |
40+
| 3 | 108939 |
41+
| 2 | 12747 |
42+
| 8 | 87709 |
43+
| 6 | 91796 |
44+
45+
### **Output:**
46+
| category | accounts_count |
47+
| -------------- | -------------- |
48+
| Low Salary | 1 |
49+
| Average Salary | 0 |
50+
| High Salary | 3 |
51+
52+
### **Explanation:**
53+
- **Low Salary**: Account with income 12747.
54+
- **Average Salary**: No accounts have an income in the range [20000, 50000].
55+
- **High Salary**: Accounts with incomes 108939, 87709, and 91796.
56+
57+
---
58+
59+
## 🖥 SQL Solution
60+
61+
### ✅ **Approach:**
62+
1. **CTE "S"**: Create a static table with the three salary categories.
63+
```sql
64+
WITH S AS (
65+
SELECT 'Low Salary' AS category
66+
UNION
67+
SELECT 'Average Salary'
68+
UNION
69+
SELECT 'High Salary'
70+
),
71+
```
72+
- This defines the three salary categories to ensure every category appears in the final result.
73+
74+
2. **CTE "T"**: Categorize each account from the **Accounts** table using a `CASE` statement and count the number of accounts in each category.
75+
```sql
76+
T AS (
77+
SELECT
78+
CASE
79+
WHEN income < 20000 THEN 'Low Salary'
80+
WHEN income > 50000 THEN 'High Salary'
81+
ELSE 'Average Salary'
82+
END AS category,
83+
COUNT(1) AS accounts_count
84+
FROM Accounts
85+
GROUP BY 1
86+
)
87+
```
88+
- The `CASE` statement assigns a salary category based on the income.
89+
- `COUNT(1)` counts the number of accounts in each category.
90+
91+
3. **Final SELECT with LEFT JOIN**: Combine the static category table `S` with the computed counts from `T` to ensure every category is included, using `IFNULL` to convert any missing count to 0.
92+
```sql
93+
SELECT S.category, IFNULL(T.accounts_count, 0) AS accounts_count
94+
FROM S
95+
LEFT JOIN T USING (category);
96+
```
97+
98+
### **Complete SQL Query:**
99+
```sql
100+
WITH S AS (
101+
SELECT 'Low Salary' AS category
102+
UNION
103+
SELECT 'Average Salary'
104+
UNION
105+
SELECT 'High Salary'
106+
),
107+
T AS (
108+
SELECT
109+
CASE
110+
WHEN income < 20000 THEN 'Low Salary'
111+
WHEN income > 50000 THEN 'High Salary'
112+
ELSE 'Average Salary'
113+
END AS category,
114+
COUNT(1) AS accounts_count
115+
FROM Accounts
116+
GROUP BY 1
117+
)
118+
SELECT S.category, IFNULL(T.accounts_count, 0) AS accounts_count
119+
FROM S
120+
LEFT JOIN T USING (category);
121+
```
122+
123+
---
124+
125+
## 🐍 Python (Pandas) Solution
126+
127+
### **Approach:**
128+
1. **Categorize Accounts**: Create a new column `category` in the DataFrame by applying the salary conditions.
129+
2. **Group and Count**: Group by the `category` column and count the number of accounts.
130+
3. **Merge with Static Categories**: Ensure all three salary categories appear by merging with a predefined DataFrame that contains all categories, filling missing counts with 0.
131+
132+
```python
133+
import pandas as pd
134+
135+
def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame:
136+
# Define the salary categorization function
137+
def categorize(income):
138+
if income < 20000:
139+
return 'Low Salary'
140+
elif income > 50000:
141+
return 'High Salary'
142+
else:
143+
return 'Average Salary'
144+
145+
# Apply categorization
146+
accounts['category'] = accounts['income'].apply(categorize)
147+
148+
# Count accounts in each category
149+
counts = accounts.groupby('category').size().reset_index(name='accounts_count')
150+
151+
# Define static categories DataFrame
152+
categories = pd.DataFrame({
153+
'category': ['Low Salary', 'Average Salary', 'High Salary']
154+
})
155+
156+
# Merge to ensure all categories are present, fill missing values with 0
157+
result = categories.merge(counts, on='category', how='left')
158+
result['accounts_count'] = result['accounts_count'].fillna(0).astype(int)
159+
160+
return result
161+
162+
# Example usage:
163+
# df = pd.read_csv("sample_accounts.csv")
164+
# print(count_salary_categories(df))
165+
```
166+
167+
---
168+
169+
## 📁 File Structure
170+
```
171+
📂 Count-Salary-Categories
172+
│── README.md
173+
│── solution.sql
174+
│── solution_pandas.py
175+
│── test_cases.sql
176+
│── sample_accounts.csv
177+
```
178+
179+
---
180+
181+
## 🔗 Useful Links
182+
- 📖 [LeetCode Problem](https://leetcode.com/problems/count-salary-categories/)
183+
- 📝 [MySQL WITH Clause (CTE)](https://www.w3schools.com/sql/sql_with.asp)
184+
- 🔍 [MySQL IFNULL Function](https://www.w3schools.com/sql/func_mysql_ifnull.asp)
185+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
186+
```
187+
188+
---
189+
190+
### Features of this `README.md`:
191+
- **Clear Problem Statement** with table structure and detailed example.
192+
- **SQL Solution** with detailed explanation using CTEs and LEFT JOIN.
193+
- **Python (Pandas) Solution** with step-by-step categorization and merging.
194+
- **Organized File Structure** for repository management.
195+
- **Helpful External Links** for further learning.
196+
197+
Let me know if you need any modifications or additional details!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /