Skip to content

Commit dd5e4ce

Browse files
authored
Create average-salary-departments-vs-company.sql
1 parent 8581f03 commit dd5e4ce

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
SELECT department_salary.pay_month, department_id,
5+
CASE
6+
WHEN department_avg < company_avg THEN 'lower'
7+
WHEN department_avg > company_avg THEN 'higher'
8+
ELSE 'same'
9+
END AS comparison
10+
FROM
11+
(
12+
SELECT department_id, AVG(amount) AS department_avg, date_format(pay_date, '%Y-%m') AS pay_month
13+
FROM salary JOIN employee ON salary.employee_id = employee.employee_id
14+
GROUP BY department_id, pay_month
15+
) AS department_salary
16+
JOIN
17+
(
18+
SELECT AVG(amount) AS company_avg, date_format(pay_date, '%Y-%m') AS pay_month
19+
FROM salary
20+
GROUP BY date_format(pay_date, '%Y-%m')
21+
) AS company_salary
22+
ON department_salary.pay_month = company_salary.pay_month
23+
;

0 commit comments

Comments
 (0)