Median Employee Salary Problem


Description

LeetCode Problem 569.

The Employee table holds all employees. The employee table has three columns: Employee Id, Company Name, and Salary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
+-----+------------+--------+
|Id   | Company    | Salary |
+-----+------------+--------+
|1    | A          | 2341   |
|2    | A          | 341    |
|3    | A          | 15     |
|4    | A          | 15314  |
|5    | A          | 451    |
|6    | A          | 513    |
|7    | B          | 15     |
|8    | B          | 13     |
|9    | B          | 1154   |
|10   | B          | 1345   |
|11   | B          | 1221   |
|12   | B          | 234    |
|13   | C          | 2345   |
|14   | C          | 2645   |
|15   | C          | 2645   |
|16   | C          | 2652   |
|17   | C          | 65     |
+-----+------------+--------+

Write a SQL query to find the median salary of each company. Bonus points if you can solve it without using any built-in SQL functions.

1
2
3
4
5
6
7
8
9
+-----+------------+--------+
|Id   | Company    | Salary |
+-----+------------+--------+
|5    | A          | 451    |
|6    | A          | 513    |
|12   | B          | 234    |
|9    | B          | 1154   |
|14   | C          | 2645   |
+-----+------------+--------+


MySQL Solution

1
2
3
4
5
6
select t1.Id, t1.Company, t1.Salary
from (select Id, Company, Salary, 
      dense_rank() over (partition by Company order by Salary, Id) as Sort, 
      count(1) over (partition by Company) / 2.0 as Counts
from Employee ) t1
where Sort between Counts and Counts + 1




Related Posts

Median Employee Salary Problem

LeetCode 569. Write a SQL query to find the median...

Trips And Users Problem

LeetCode 262. Write a SQL query to find the cancellation...

Game Play Analysis IV Problem

LeetCode 550. Write an SQL query that reports the fraction...

Game Play Analysis III Problem

LeetCode 534. Write an SQL query that reports for each...

Game Play Analysis II Problem

LeetCode 512. Write a SQL query that reports the device...

Game Play Analysis I Problem

LeetCode 511. Write an SQL query that reports the first...