The "Count Salary Categories interview question" is a SQL data reporting challenge. You are given an Accounts table with employee IDs and their monthly salaries. You need to categorize each account into one of three buckets: "Low Salary" (less than 20,000), "Average Salary" (20,000 to 50,000 inclusive), and "High Salary" (greater than 50,000). The final report must show all three categories and the count of accounts in each, even if a category has zero accounts.
Companies like Amazon and Bloomberg use the "Count Salary Categories coding problem" to test a candidate's ability to perform Conditional Aggregation and handle "missing" data categories. A simple GROUP BY will only return categories that exist in the data. This problem evaluates whether you know how to use UNION or a reference table to ensure all required labels appear in the result.
This problem follows the Union of Aggregates pattern.
SELECT statements, one for each category.
SELECT 'Low Salary' as category, COUNT(*) as accounts_count FROM Accounts WHERE income < 20000UNION ALL.Table: (1, 10000), (2, 60000), (3, 70000)
CASE WHEN inside a GROUP BY. This will skip categories that have zero entries (like "Average Salary" in the example above).When a SQL problem requires returning labels that might not exist in the source data, think UNION ALL. It is the most robust way to "force" specific rows into your output. This is a common "Database interview pattern" for financial reporting.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Confirmation Rate | Medium | Solve | |
| Product Sales Analysis III | Medium | Solve | |
| Rank Scores | Medium | Solve | |
| Product Price at a Given Date | Medium | Solve | |
| Friend Requests II: Who Has the Most Friends | Medium | Solve |