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 NameType
account_idint
incomeint

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
Bank Account Income Classification SystemLow Salary Bin< $20,000$12KCount: 1Average Salary Bin$20K - $50K$45K$25KCount: 2High Salary Bin> $50,000$108K$87KCount: 2Single Pass AlgorithmO(n) time, O(1) space๐ŸŽฏ Result: All categories reported, even if count = 0Low: 1 | Average: 2 | High: 2
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing three counter variables

n
2n
โœ“ 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)
Asked in
Amazon 28 Google 22 Meta 18 Microsoft 15
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen