Skip to content
BytePatterns

Highest Paid in Every Department

MediumSQL#correlated-subquery#max#ties~20m

Problem

A table employee(id, name, dept, salary) holds one row per person. Write a query that returns, for every department, the people who earn that department's highest salary, as columns dept, name and salary. When several people share the top salary of a department, return all of them. Sort by dept, then name.

Examples

Input:  employee = [(1, "Ana", "eng", 120), (2, "Ben", "eng", 150), (3, "Cy", "ops", 90), (4, "Dee", "ops", 80)]
Output: [("eng", "Ben", 150), ("ops", "Cy", 90)]
Input:  employee = [(1, "Ana", "eng", 150), (2, "Ben", "eng", 150), (3, "Cy", "eng", 100)]
Output: [("eng", "Ana", 150), ("eng", "Ben", 150)]
Why:    Ana and Ben tie for the top of eng, so both come back
Input:  employee = [(1, "Gus", "hr", 70)]
Output: [("hr", "Gus", 70)]
Why:    edge case, the only person in a department is its top earner

Hints

0 / 3

Stuck on the idea rather than the code? Subqueries covers it.