Skip to content
We areTOP 1%

Department Highest Salary

MediumLeetCodeSQL: Subqueries & CTEs
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Hint and exampleShow

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

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account