Skip to content

Five longest-serving employees, keeping ties at the cut-off

EasyFull bankRanking and top N8 min2 hidden datasets

The task

The people team is planning a service-award event for the longest-serving current employees (termination_date is NULL). Rank them by hire_date, earliest first. People hired on the same day share a rank, and the next rank skips ahead (1, 1, 3).

Keep everyone whose rank is 5 or better. If several people share the cut-off, all of them are kept, so more than 5 people can be returned. Include people without a department.

Return full_name, hire_date and service_rank. Sort by service_rank ascending, then full_name ascending.

Expected output

full_namehire_dateservice_rank
  • Sorted by service_rank ascending, 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.

intUnique id.
textName.
textMain office city.
employees120 rows in the sample

Everyone who has worked here, including people who left. Key: employee_id.

intUnique id.
textName.
intDepartment. NULL for a few people between teams.
intTheir manager (an employee_id). NULL for the CEO.
textTitle.
dateFirst day.
dateLast 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.

intThe employee.
dateFirst day of this salary.
numeric(10,2)Yearly salary, USD.
reviews301 rows in the sample

Year-end review ratings. Key: employee_id, review_year.

intThe employee.
intYear reviewed.
int1 (low) to 5 (high).

Related questions

Loading the editor and PostgreSQL