Count Salary Categories - Problem
You're working as a data analyst for a bank and need to create a salary distribution report. Given a table of bank accounts with their monthly incomes, you must categorize each account into salary brackets and count how many accounts fall into each category.
Table Schema: Accounts
| Column Name | Type |
|---|---|
| account_id | int |
| income | int |
Salary Categories:
- "Low Salary": Income strictly less than $20,000
- "Average Salary": Income between $20,000 and $50,000 (inclusive)
- "High Salary": Income strictly greater than $50,000
Important: Your result must include all three categories, even if some have zero accounts. Return the count for each salary category.
Input & Output
example_1.sql โ Standard Case
$
Input:
Accounts table:
+------------+--------+
| account_id | income |
+------------+--------+
| 3 | 108000 |
| 2 | 12000 |
| 8 | 87000 |
| 6 | 45000 |
+------------+--------+
โบ
Output:
+----------------+----------------+
| category | accounts_count |
+----------------+----------------+
| Low Salary | 1 |
| Average Salary | 1 |
| High Salary | 2 |
+----------------+----------------+
๐ก Note:
Account 2 has income $12,000 (< $20,000) โ Low Salary. Account 6 has income $45,000 (between $20,000-$50,000) โ Average Salary. Accounts 3 and 8 have incomes $108,000 and $87,000 (> $50,000) โ High Salary.
example_2.sql โ All Same Category
$
Input:
Accounts table:
+------------+--------+
| account_id | income |
+------------+--------+
| 1 | 25000 |
| 2 | 35000 |
| 3 | 40000 |
+------------+--------+
โบ
Output:
+----------------+----------------+
| category | accounts_count |
+----------------+----------------+
| Low Salary | 0 |
| Average Salary | 3 |
| High Salary | 0 |
+----------------+----------------+
๐ก Note:
All accounts have salaries between $20,000-$50,000, so they all fall into Average Salary category. Low and High categories have 0 accounts, but must still be included in the result.
example_3.sql โ Boundary Values
$
Input:
Accounts table:
+------------+--------+
| account_id | income |
+------------+--------+
| 1 | 19999 |
| 2 | 20000 |
| 3 | 50000 |
| 4 | 50001 |
+------------+--------+
โบ
Output:
+----------------+----------------+
| category | accounts_count |
+----------------+----------------+
| Low Salary | 1 |
| Average Salary | 2 |
| High Salary | 1 |
+----------------+----------------+
๐ก Note:
Account 1: $19,999 is strictly less than $20,000 โ Low Salary. Accounts 2 and 3: $20,000 and $50,000 are within inclusive range [$20,000, $50,000] โ Average Salary. Account 4: $50,001 is strictly greater than $50,000 โ High Salary.
Visualization
Tap to expand
Understanding the Visualization
1
Setup Categories
Prepare three bins: Low (<$20K), Average ($20K-$50K), High (>$50K)
2
Process Each Account
For each account, check the income and place in appropriate bin
3
Count Results
Count items in each bin, ensuring all three categories are reported
Key Takeaway
๐ฏ Key Insight: Use conditional aggregation to count all salary categories in a single database scan, ensuring optimal O(n) performance while guaranteeing all three categories appear in results.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all records with conditional counting
โ Linear Growth
Space Complexity
O(1)
Only storing three counter variables
โ Linear Space
Constraints
- 1 โค Number of accounts โค 104
- 0 โค income โค 106
- All three salary categories must be included in the result
- account_id is the primary key (unique values)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code