← Top-N per group

Top 40% by salary in a department: departments 10–19, active

Level: Middle100 XP

Instructions

Consider only active employees (fired_at IS NULL) from the departments with department_id from 10 to 19 inclusive. For each employee compute cume_dist() within their department by salary descending: the share of the department's employees whose salary is at least as high as theirs (equal salaries get the same value). Output the employees whose cume_dist is at most 0.4 (the top 40% of the department; everyone with an equal salary at the boundary is included or excluded together). Columns: department (the department name), name (the employee's name), salary. Row order does not matter.

Hints

Each next one is more detailed and costs more

Log in to open hints

Query

Result

Click Run to see the query result, or Check to submit your solution.

Dataset schema

employees.department_id (0…N) ↔ departments.id (1…1)employees.manager_id (0…N) ↔ employees.id (0…1)departments.parent_id (0…N) ↔ departments.id (0…1)salary_history.employee_id (0…N) ↔ employees.id (1…1)departmentsPKidintegerFKparent_idintegeremployeesPKidintegerFKdepartment_idintegerFKmanager_idintegersalary_historyPKFKemployee_idintegerPKchanged_atdate
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneemployees.department_id → departments.id
  • N:1Many-to-oneemployees.manager_id → employees.id
  • N:1Many-to-onedepartments.parent_id → departments.id
  • N:1Many-to-onesalary_history.employee_id → employees.id

Table data

PostgreSQL

Select a table in the diagram or list to view its rows.

Dataset schema