Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
Intermediate · 25 XP

Find Department Top Earners with DENSE_RANK

Write a SQL query to find all employees who earn the highest or second highest salary in their respective departments. Table `employees` columns: - `id` (INT) - `name` (VARCHAR) - `salary` (INT) - `department_id` (INT) Return columns `department_id`, `name`, `salary` ordered by `department_id ASC`, `salary DESC`.

Show Step-by-Step Hint
  • • Partition by department_id and order by salary DESC.
  • • Wrap the window function in a CTE to filter where rank <= 2.
Type: Sql
Runtime: SQL
SQL solutionCHALLENGE #6
Solutions and scores are stored in your private attempt history.Sign in to submit challenge