Skip to content
Onsite

Highest-paid employees in each department

MediumP1Coding

Tables:

department

ColumnType
idint (primary key)
nametext

employee

ColumnType
idint (primary key)
nametext
salaryint
department_idint (references department.id)

Write a query that returns the highest-paid employee(s) of each department. If several employees tie for the top salary in a department, return all of them. Return the columns department (the department name), employee and salary.

Departments without employees are not in the result.

Schema

CREATE TABLE department (
  id int PRIMARY KEY,
  name text NOT NULL
);
CREATE TABLE employee (
  id int PRIMARY KEY,
  name text NOT NULL,
  salary int NOT NULL,
  department_id int NOT NULL REFERENCES department(id)
);

Example 1

Input
department: (1, 'IT'), (2, 'Sales') employee: (1, 'Ann', 90, 1), (2, 'Bob', 90, 1), (3, 'Cy', 70, 1), (4, 'Di', 60, 2)
Output
IT, Ann, 90 IT, Bob, 90 Sales, Di, 60

Constraints

  • PostgreSQL dialect.
  • Return rows in any order.

Hints

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