You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- The Employee table holds the salary information in a year.
3
+
4
+
-- Write a SQL to get the cumulative sum of an employee's salary over a period of 3 months but exclude the most recent month.
5
+
6
+
-- The result should be displayed by 'Id' ascending, and then by 'Month' descending.
7
+
8
+
-- Example
9
+
-- Input
10
+
11
+
-- | Id | Month | Salary |
12
+
-- |----|-------|--------|
13
+
-- | 1 | 1 | 20 |
14
+
-- | 2 | 1 | 20 |
15
+
-- | 1 | 2 | 30 |
16
+
-- | 2 | 2 | 30 |
17
+
-- | 3 | 2 | 40 |
18
+
-- | 1 | 3 | 40 |
19
+
-- | 3 | 3 | 60 |
20
+
-- | 1 | 4 | 60 |
21
+
-- | 3 | 4 | 70 |
22
+
-- Output
23
+
24
+
-- | Id | Month | Salary |
25
+
-- |----|-------|--------|
26
+
-- | 1 | 3 | 90 |
27
+
-- | 1 | 2 | 50 |
28
+
-- | 1 | 1 | 20 |
29
+
-- | 2 | 1 | 20 |
30
+
-- | 3 | 3 | 100 |
31
+
-- | 3 | 2 | 40 |
32
+
33
+
34
+
-- Explanation
35
+
-- Employee '1' has 3 salary records for the following 3 months except the most recent month '4': salary 40 for month '3', 30 for month '2' and 20 for month '1'
36
+
-- So the cumulative sum of salary of this employee over 3 months is 90(40+30+20), 50(30+20) and 20 respectively.
37
+
38
+
-- | Id | Month | Salary |
39
+
-- |----|-------|--------|
40
+
-- | 1 | 3 | 90 |
41
+
-- | 1 | 2 | 50 |
42
+
-- | 1 | 1 | 20 |
43
+
-- Employee '2' only has one salary record (month '1') except its most recent month '2'.
44
+
-- | Id | Month | Salary |
45
+
-- |----|-------|--------|
46
+
-- | 2 | 1 | 20 |
47
+
48
+
49
+
-- Employ '3' has two salary records except its most recent pay month '4': month '3' with 60 and month '2' with 40. So the cumulative salary is as following.
50
+
-- | Id | Month | Salary |
51
+
-- |----|-------|--------|
52
+
-- | 3 | 3 | 100 |
53
+
-- | 3 | 2 | 40 |
54
+
55
+
-- Solution
56
+
with t1 as(
57
+
select*, max(month) over(partition by id) as recent_month
58
+
from employee)
59
+
60
+
select id, month, sum(salary) over(partition by id order by month rows between 2 preceding and current row) as salary
61
+
from t1
62
+
where month<recent_month
63
+
order by1, 2desc
64
+
Collapse file: Hard/Department top three salaries.sql
-- The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.
3
+
4
+
-- +----+-------+--------+--------------+
5
+
-- | Id | Name | Salary | DepartmentId |
6
+
-- +----+-------+--------+--------------+
7
+
-- | 1 | Joe | 85000 | 1 |
8
+
-- | 2 | Henry | 80000 | 2 |
9
+
-- | 3 | Sam | 60000 | 2 |
10
+
-- | 4 | Max | 90000 | 1 |
11
+
-- | 5 | Janet | 69000 | 1 |
12
+
-- | 6 | Randy | 85000 | 1 |
13
+
-- | 7 | Will | 70000 | 1 |
14
+
-- +----+-------+--------+--------------+
15
+
-- The Department table holds all departments of the company.
16
+
17
+
-- +----+----------+
18
+
-- | Id | Name |
19
+
-- +----+----------+
20
+
-- | 1 | IT |
21
+
-- | 2 | Sales |
22
+
-- +----+----------+
23
+
-- Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows (order of rows does not matter).
24
+
25
+
-- +------------+----------+--------+
26
+
-- | Department | Employee | Salary |
27
+
-- +------------+----------+--------+
28
+
-- | IT | Max | 90000 |
29
+
-- | IT | Randy | 85000 |
30
+
-- | IT | Joe | 85000 |
31
+
-- | IT | Will | 70000 |
32
+
-- | Sales | Henry | 80000 |
33
+
-- | Sales | Sam | 60000 |
34
+
-- +------------+----------+--------+
35
+
-- Explanation:
36
+
37
+
-- In IT department, Max earns the highest salary, both Randy and Joe earn the second highest salary,
38
+
-- and Will earns the third highest salary.
39
+
-- There are only two employees in the Sales department,
40
+
-- Henry earns the highest salary while Sam earns the second highest salary.
-- This table has the info of the users of an online shopping website where users can sell and buy items.
13
+
-- Table: Orders
14
+
15
+
-- +---------------+---------+
16
+
-- | Column Name | Type |
17
+
-- +---------------+---------+
18
+
-- | order_id | int |
19
+
-- | order_date | date |
20
+
-- | item_id | int |
21
+
-- | buyer_id | int |
22
+
-- | seller_id | int |
23
+
-- +---------------+---------+
24
+
-- order_id is the primary key of this table.
25
+
-- item_id is a foreign key to the Items table.
26
+
-- buyer_id and seller_id are foreign keys to the Users table.
27
+
-- Table: Items
28
+
29
+
-- +---------------+---------+
30
+
-- | Column Name | Type |
31
+
-- +---------------+---------+
32
+
-- | item_id | int |
33
+
-- | item_brand | varchar |
34
+
-- +---------------+---------+
35
+
-- item_id is the primary key of this table.
36
+
37
+
38
+
-- Write an SQL query to find for each user, whether the brand of the second item (by date) they sold is their favorite brand. If a user sold less than two items, report the answer for that user as no.
39
+
40
+
-- It is guaranteed that no seller sold more than one item on a day.
41
+
42
+
-- The query result format is in the following example:
0 commit comments