Skip to content

Commit f9d3ac4

Browse files
authored
MEDIUM
1 parent 08504c9 commit f9d3ac4

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

1907. Count Salary Categories.sql

+58
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
1+
-- Problem:
2+
3+
/*
4+
Table: Accounts
5+
6+
+-------------+------+
7+
| Column Name | Type |
8+
+-------------+------+
9+
| account_id | int |
10+
| income | int |
11+
+-------------+------+
12+
account_id is the primary key (column with unique values) for this table.
13+
Each row contains information about the monthly income for one bank account.
14+
15+
16+
Write a solution to calculate the number of bank accounts for each salary category. The salary categories are:
17+
18+
"Low Salary": All the salaries strictly less than $20000.
19+
"Average Salary": All the salaries in the inclusive range [$20000, $50000].
20+
"High Salary": All the salaries strictly greater than $50000.
21+
The result table must contain all three categories. If there are no accounts in a category, return 0.
22+
23+
Return the result table in any order.
24+
25+
The result format is in the following example.
26+
27+
28+
29+
Example 1:
30+
31+
Input:
32+
Accounts table:
33+
+------------+--------+
34+
| account_id | income |
35+
+------------+--------+
36+
| 3 | 108939 |
37+
| 2 | 12747 |
38+
| 8 | 87709 |
39+
| 6 | 91796 |
40+
+------------+--------+
41+
Output:
42+
+----------------+----------------+
43+
| category | accounts_count |
44+
+----------------+----------------+
45+
| Low Salary | 1 |
46+
| Average Salary | 0 |
47+
| High Salary | 3 |
48+
+----------------+----------------+
49+
Explanation:
50+
Low Salary: Account 2.
51+
Average Salary: No accounts.
52+
High Salary: Accounts 3, 6, and 8.
53+
*/
54+
55+
-------------------------------------------------------------------------------
56+
57+
-- Solution:
58+
159
SELECT "Low Salary" AS category , SUM(IF(income<20000,1,0)) AS accounts_count FROM Accounts
260
UNION
361
SELECT "Average Salary" AS category , SUM(IF(income>=20000 AND income<=50000,1,0)) AS accounts_count FROM Accounts

0 commit comments

Comments
 (0)