-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcount-salary-categories.sql
103 lines (92 loc) · 2.64 KB
/
count-salary-categories.sql
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
1907. Count Salary Categories
Level
Medium
Description
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. Return the result table in **any order.
The query result format is in the following example.
Accounts table:
+------------+--------+
| account_id | income |
+------------+--------+
| 3 | 108939 |
| 2 | 12747 |
| 8 | 87709 |
| 6 | 91796 |
+------------+--------+
Result table:
+----------------+----------------+
| category | accounts_count |
+----------------+----------------+
| Low Salary | 1 |
| Average Salary | 0 |
| High Salary | 3 |
+----------------+----------------+
*/
# V0
SELECT 'Low Salary' AS category,
COUNT(*) AS accounts_count
FROM Accounts
WHERE income < 20000
UNION ALL
SELECT 'Average Salary' AS category,
COUNT(*) AS accounts_count
FROM Accounts
WHERE income BETWEEN 20000 AND 50000
UNION ALL
SELECT 'High Salary' AS category,
COUNT(*) AS accounts_count
FROM Accounts
WHERE income > 50000;
# V1
# https://leetcode.ca/2021-07-29-1907-Count-Salary-Categories/
# Write your MySQL query statement below
select t.category, ifnull(a.cnt, 0) as accounts_count
from (
select 'Low Salary' category
union
select 'Average Salary'
union
select 'High Salary'
) as t
left join (
select
case when income < 20000 then 'Low Salary'
when income > 50000 then 'High Salary'
else 'Average Salary' end
as category,
count(1) as cnt
from Accounts
group by 1
) as a
on t.category = a.category;
# V2
# Time: O(n)
# Space: O(n)
SELECT 'Low Salary' AS category,
COUNT(*) AS accounts_count
FROM Accounts
WHERE income < 20000
UNION ALL
SELECT 'Average Salary' AS category,
COUNT(*) AS accounts_count
FROM Accounts
WHERE income BETWEEN 20000 AND 50000
UNION ALL
SELECT 'High Salary' AS category,
COUNT(*) AS accounts_count
FROM Accounts
WHERE income > 50000;