Highest-paid employees in each department
MediumP1Coding
Tables:
department
| Column | Type |
|---|---|
| id | int (primary key) |
| name | text |
employee
| Column | Type |
|---|---|
| id | int (primary key) |
| name | text |
| salary | int |
| department_id | int (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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 2 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.