Average Salary: Departments VS Company - Problem
Average Salary: Departments VS Company
You're working as a data analyst for a large corporation and need to create a monthly salary comparison report. Your task is to compare each department's average salary with the company's overall average salary for every month.
Given two tables:
•
•
•
•
•
•
Your goal: For each month and department, determine if the department's average salary is "higher", "lower", or "same" compared to the company's average salary for that same month.
Output format: Return columns for
You're working as a data analyst for a large corporation and need to create a monthly salary comparison report. Your task is to compare each department's average salary with the company's overall average salary for every month.
Given two tables:
Salary table contains monthly salary records:•
id: Primary key•
employee_id: References Employee table•
amount: Salary amount for that month•
pay_date: The month/year of paymentEmployee table contains department assignments:•
employee_id: Primary key•
department_id: Which department the employee belongs toYour goal: For each month and department, determine if the department's average salary is "higher", "lower", or "same" compared to the company's average salary for that same month.
Output format: Return columns for
pay_month, department_id, and comparison (higher/lower/same). Input & Output
example_1.sql — Basic Scenario
$
Input:
Salary table:
| id | employee_id | amount | pay_date |
|----|-------------|--------|------------|
| 1 | 1 | 9000 | 2017-03-31 |
| 2 | 2 | 6000 | 2017-03-31 |
| 3 | 3 | 10000 | 2017-03-31 |
| 4 | 1 | 7000 | 2017-02-28 |
| 5 | 2 | 6000 | 2017-02-28 |
| 6 | 3 | 8000 | 2017-02-28 |
Employee table:
| employee_id | department_id |
|-------------|---------------|
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
›
Output:
| pay_month | department_id | comparison |
|-----------|---------------|------------|
| 2017-02 | 1 | lower |
| 2017-02 | 2 | same |
| 2017-03 | 1 | lower |
| 2017-03 | 2 | higher |
💡 Note:
For March 2017: Company avg = (9000+6000+10000)/3 = 8333.33. Dept 1 avg = (9000+6000)/2 = 7500 (lower), Dept 2 avg = 10000 (higher). For February 2017: Company avg = (7000+6000+8000)/3 = 7000. Dept 1 avg = (7000+6000)/2 = 6500 (lower), Dept 2 avg = 8000 (higher).
example_2.sql — Equal Averages
$
Input:
Salary table:
| id | employee_id | amount | pay_date |
|----|-------------|--------|------------|
| 1 | 1 | 8000 | 2017-03-31 |
| 2 | 2 | 9000 | 2017-03-31 |
| 3 | 3 | 8000 | 2017-03-31 |
| 4 | 4 | 9000 | 2017-03-31 |
Employee table:
| employee_id | department_id |
|-------------|---------------|
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
›
Output:
| pay_month | department_id | comparison |
|-----------|---------------|------------|
| 2017-03 | 1 | same |
| 2017-03 | 2 | same |
💡 Note:
Company average = (8000+9000+8000+9000)/4 = 8500. Department 1 average = (8000+9000)/2 = 8500. Department 2 average = (8000+9000)/2 = 8500. Both departments have the same average as the company.
example_3.sql — Single Employee Departments
$
Input:
Salary table:
| id | employee_id | amount | pay_date |
|----|-------------|--------|------------|
| 1 | 1 | 12000 | 2017-03-31 |
| 2 | 2 | 5000 | 2017-03-31 |
| 3 | 3 | 7000 | 2017-03-31 |
Employee table:
| employee_id | department_id |
|-------------|---------------|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
›
Output:
| pay_month | department_id | comparison |
|-----------|---------------|------------|
| 2017-03 | 1 | higher |
| 2017-03 | 2 | lower |
| 2017-03 | 3 | lower |
💡 Note:
Company average = (12000+5000+7000)/3 = 8000. Department 1: 12000 (higher than 8000). Department 2: 5000 (lower than 8000). Department 3: 7000 (lower than 8000). This shows how individual departments can vary significantly from company average.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Monthly Data
Gather all salary records for each month, similar to collecting all test scores
2
Calculate Company Average
Compute the overall company average salary for each month (like school average)
3
Calculate Department Averages
Compute average salary for each department in each month (like class averages)
4
Compare and Label
Compare each department's average to company average and label as higher/lower/same
Key Takeaway
🎯 Key Insight: Use window functions to calculate the company average once per month and share it across all department comparisons, avoiding expensive repeated subqueries.
Time & Space Complexity
Time Complexity
O(n log n)
n salary records, dominated by sorting for window functions and GROUP BY
⚡ Linearithmic
Space Complexity
O(m * d)
Space for results where m is months and d is departments
✓ Linear Space
Constraints
- 1 ≤ employee_id ≤ 104
- 1 ≤ department_id ≤ 100
- 1 ≤ salary amount ≤ 106
- pay_date format is YYYY-MM-DD
- Each employee belongs to exactly one department
- Employee can have multiple salary records (different months)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code