Skip to content
Onsite

Median salary per company

MediumP2Coding

Table employee:

ColumnType
idint (primary key)
companytext
salaryint

Return the median salary for each company. When a company has an even number of employees, the median is the average of the two middle salaries.

Output columns: company, median_salary.

Schema

CREATE TABLE employee (
  id int PRIMARY KEY,
  company text NOT NULL,
  salary int NOT NULL
);

Example 1

Input
employee: (1, A, 100), (2, A, 300), (3, A, 200)
Output
A: 200

Example 2

Input
employee: (1, B, 100), (2, B, 200), (3, B, 300), (4, B, 500)
Output
B: 250

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 6.