Skip to content

569. Median Employee Salary

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
WITH
  EmployeesWithRowNumberInCompany AS (
    SELECT
      id,
      company,
      salary,
      ROW_NUMBER() OVER(
        PARTITION BY company
        ORDER BY salary
      ) AS `row_number`,
      COUNT(*) OVER(PARTITION BY company) AS `count`
    FROM Employee
  )
SELECT id, company, salary
FROM EmployeesWithRowNumberInCompany
WHERE
  `row_number` >= `count` / 2
  AND `row_number` <= `count` / 2 + 1;