Median Employee Salary - Problem
You're working as a data analyst for a large corporation that tracks employee salaries across multiple companies. Your task is to find the median salary employees for each company in the database.
Given an Employee table with the following structure:
| Column Name | Type |
|---|---|
| id | int |
| company | varchar |
| salary | int |
Your goal is to write a SQL query that returns the complete employee records (not just salary values) for employees whose salary represents the median for their respective company.
Important rules:
- When sorting salaries to find the median, break ties by employee
id(ascending order) - For companies with an odd number of employees, return the middle employee
- For companies with an even number of employees, return both middle employees
- The result can be returned in any order
This problem tests your understanding of window functions, ranking, and median calculation in SQL databases.
Input & Output
example_1.sql โ Basic Case
$
Input:
Employee table:
| id | company | salary |
|----|---------|--------|
| 1 | A | 2341 |
| 2 | A | 341 |
| 3 | A | 15000 |
| 4 | A | 15000 |
| 5 | A | 15000 |
| 6 | A | 15000 |
โบ
Output:
| id | company | salary |
|----|---------|--------|
| 2 | A | 341 |
| 3 | A | 15000 |
๐ก Note:
Company A has 6 employees. When sorted by salary (then by id): [341, 2341, 15000, 15000, 15000, 15000]. For 6 employees, median positions are 3 and 4. Position 3 is employee id=2 (341), position 4 is employee id=3 (15000, lowest id among ties).
example_2.sql โ Multiple Companies
$
Input:
Employee table:
| id | company | salary |
|----|---------|--------|
| 1 | A | 100 |
| 2 | A | 200 |
| 3 | A | 300 |
| 4 | B | 400 |
| 5 | B | 500 |
| 6 | B | 600 |
| 7 | B | 700 |
โบ
Output:
| id | company | salary |
|----|---------|--------|
| 2 | A | 200 |
| 5 | B | 500 |
| 6 | B | 600 |
๐ก Note:
Company A (3 employees): median is position 2 โ id=2 (salary 200). Company B (4 employees): median positions are 2 and 3 โ id=5 (salary 500) and id=6 (salary 600).
example_3.sql โ Tie Breaking by ID
$
Input:
Employee table:
| id | company | salary |
|----|---------|--------|
| 1 | A | 100 |
| 4 | A | 100 |
| 3 | A | 100 |
โบ
Output:
| id | company | salary |
|----|---------|--------|
| 3 | A | 100 |
๐ก Note:
All employees have same salary (100), so we sort by id: [1, 3, 4]. For 3 employees, median position is 2, which corresponds to id=3.
Constraints
- 1 โค number of employees โค 104
- 1 โค employee.id โค 104
- 1 โค employee.salary โค 106
- employee.id is unique across all companies
- Company names are non-empty strings with length โค 100
Visualization
Tap to expand
Understanding the Visualization
1
Group by Company
Separate employees into company-specific groups, like sorting cards by suit
2
Sort Within Groups
Within each company, arrange employees by salary (then ID), like ordering cards by value
3
Number the Positions
Assign position numbers 1, 2, 3... to each employee in their sorted company group
4
Calculate Median Range
For n employees: median positions are at (n+1)/2 and (n+2)/2
5
Select Median Employees
Return employees whose position numbers fall within the median range
Key Takeaway
๐ฏ Key Insight: Window functions allow us to partition, sort, and rank data in a single pass, making median calculation efficient by using mathematical position ranges instead of complex nested queries.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code