Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Earners by Department with Window Functions

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top Earners by Department with Window Functions. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Brillio's People Analytics team needs a department-level view of its highest-paid active employees. Write a PostgreSQL query that ranks employees within each department using a window function and returns the top five.

Requirements

  1. Join employees to departments using department_id.
  2. Include only employees whose employment_status is Active and whose salary is not NULL.
  3. Use a window function partitioned by department, ranking salary from highest to lowest. Use employee_id as a deterministic tie-breaker.
  4. Return no more than five employees per department, ordered by department and rank.

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
department_idINTReferences departments.department_id
salaryNUMERIC(12,2)Annual salary
employment_statusVARCHAR(20)Current employment status
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results