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 NameType
idint
companyvarchar
salaryint

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
Median Employee Salary - Visual SolutionStep 1: Raw Employee DataID:1 A:50kID:2 B:60kID:3 A:40kID:4 B:70kID:5 A:60kID:6 B:50kStep 2: Partition by CompanyCompany AID:3 40kID:1 50kID:5 60kRN:1RN:2 โœ“RN:3Company BID:6 50kID:2 60kID:4 70kRN:1RN:2 โœ“RN:3Step 3: Window Function LogicROW_NUMBER() OVER (PARTITION BY company ORDER BY salary, id)Company A (3 employees):โ€ข Median position: (3+1)/2 = 2โ€ข Employee at RN=2: ID=1 (50k)โ†’ Return ID=1Company B (3 employees):โ€ข Median position: (3+1)/2 = 2โ€ข Employee at RN=2: ID=2 (60k)โ†’ Return ID=2Final Result: Median EmployeesID=1 (Company A, 50k)ID=2 (Company B, 60k)
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.
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
28.4K Views
Medium-High Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen