Pay ladder with RANK and DENSE_RANK side by side
The task
The people team is drawing a pay ladder for every department and cannot decide how to number tied salaries, so they want both numberings next to each other.
Use current employees only (termination_date is NULL) who belong to a department. Within each department (identified by department_id: two departments in different offices can share a name, and are ranked separately), order salaries from highest to lowest and return:
department_namefull_namesalarysalary_rank: people with the same salary share a rank, and the next rank skips ahead (1, 1, 3).dense_salary_rank: people with the same salary share a rank, and the next rank follows without a gap (1, 1, 2).
Sort by department_name ascending, then salary descending, then full_name ascending.
Expected output
department_namefull_namesalarysalary_rankdense_salary_rank- Sorted by department_name ascending, then salary descending, then full_name ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables People team · click a name to insert it
departments7 rows in the sample
One row per department. Key: department_id.
| int | Unique id. | |
| text | Name. | |
| text | Main office city. |
employees120 rows in the sample
Everyone who has worked here, including people who left. Key: employee_id.
| int | Unique id. | |
| text | Name. | |
| int | Department. NULL for a few people between teams. | |
| int | Their manager (an employee_id). NULL for the CEO. | |
| text | Title. | |
| date | First day. | |
| date | Last day. NULL while employed. | |
| numeric(10,2) | Current (or final) yearly salary, USD. |
salary_changes367 rows in the sample
Every salary someone has had, from the day it applied. Key: employee_id, effective_on.
| int | The employee. | |
| date | First day of this salary. | |
| numeric(10,2) | Yearly salary, USD. |
reviews301 rows in the sample
Year-end review ratings. Key: employee_id, review_year.
| int | The employee. | |
| int | Year reviewed. | |
| int | 1 (low) to 5 (high). |
Related questions
- Employees in the top quarter of pay, by PERCENT_RANKmedium
- Five longest-serving employees, keeping ties at the cut-offeasy
- Second highest salary in each departmentmedium
- Top earner in each department, with a tie-breakereasy
- Average first purchase value by campaign, organic includedmedium
- Best seller on each sales channel in the first quartermedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL