-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountSalaryCategories1907.sql
More file actions
49 lines (43 loc) · 1.62 KB
/
countSalaryCategories1907.sql
File metadata and controls
49 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
1907. Count Salary Categories, Medium level: https://leetcode.com/problems/count-salary-categories/
Table: Accounts
+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id | int |
| income | int |
+-------------+------+
account_id is the primary key for this table.
Each row contains information about the monthly income for one bank account.
Write an SQL query to report the number of bank accounts of each salary category. The salary categories are:
"Low Salary": All the salaries strictly less than $20000.
"Average Salary": All the salaries in the inclusive range [$20000, $50000].
"High Salary": All the salaries strictly greater than $50000.
The result table must contain all three categories. If there are no accounts in a category, then report 0.
*/
-- To avoid not having a 0 in final answer, use 3 UNION statements is easy to understand but likely slower runetime:
SELECT "High Salary" as category,
sum(CASE WHEN income > 50000 THEN 1 else 0
END) as accounts_count
FROM accounts
UNION
SELECT "Low Salary" as category,
sum(CASE WHEN income < 20000 then 1 else 0
END) as accounts_count
FROM accounts
UNION
SELECT "Average Salary" as category,
sum(CASE WHEN income >= 20000 and income <= 50000 then 1 else 0 END) as accounts_count
FROM accounts
-- Passes 10/11, does not find a salary if null need to fix this bug:
with cte as (SELECT account_id,
CASE
when income < 20000 then 'Low Salary'
when income <= 50000 and income >=20000 then 'Average Salary'
when income > 50000 then 'High Salary'
END as category
FROM Accounts
)
SELECT category, count(account_id) as accounts_count
FROM cte
GROUP BY category