Department Highest Salary
MediumLeetCodeSQL: Subqueries & CTEs
Hint and exampleShowHide
Compute the maximum of each group in a subquery, then keep every row that equals its group's maximum, so that ties are all returned.
Example: products(name, category, price)
name | category | price
Pen | Office | 20
Stapler | Office | 90
Chair | Furniture | 900
Desk | Furniture | 900
SELECT category, name, price
FROM products
WHERE (category, price) IN (
SELECT category, MAX(price)
FROM products
GROUP BY category
);
-- Office | Stapler | 90, Furniture | Chair | 900, Furniture | Desk | 900
More in SQL: Subqueries & CTEs
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Customers Who Never Order | LeetCode | Easy | Solve Customers Who Never Order on LeetCode (opens in a new tab) | |||
| 2 | Second Highest Salary | LeetCode | Medium | Solve Second Highest Salary on LeetCode (opens in a new tab) | |||
| 3 | Department Highest Salary | LeetCode | Medium | Solve Department Highest Salary on LeetCode (opens in a new tab) | |||
| 4 | Customers Who Bought All Products | LeetCode | Medium | Solve Customers Who Bought All Products on LeetCode (opens in a new tab) | |||
| 5 | Investments in 2016 | LeetCode | Medium | Solve Investments in 2016 on LeetCode (opens in a new tab) |