bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 569. Median Employee Salary

569. Median Employee Salary

Leetcode Solutions

Using Window Functions to Calculate Median Salary

  1. Use the ROW_NUMBER() window function to assign a row number to each salary within each company, ordered by salary and id.
  2. Use the COUNT() window function to get the total number of employees in each company.
  3. Calculate the positions of the median salary based on the total count (if the count is odd, use (count + 1) / 2; if even, use count / 2 and count / 2 + 1).
  4. Filter the rows where the row number matches the median positions calculated in the previous step.
  5. Select the id, company, and salary columns from the filtered rows.

erDiagram
    Employee {
        int id PK "The unique identifier for an employee"
        varchar company "The company where the employee works"
        int salary "The salary of the employee"
    }

Calculating Median Salary Without Window Functions

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...