Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions and Top Salaries

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

Your question is Window Functions and Top Salaries. 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

Inc. In wants to identify the highest-paid employees within each department for compensation analysis. Write a PostgreSQL query that ranks employees independently within their departments and returns the top three salaries per department.

Requirements

  1. Join employees to departments using department_id.
  2. Exclude employees whose salary is NULL.
  3. Use a window function to rank salaries in descending order within each department, breaking ties by employee_id.
  4. Return employees ranked 1 through 3, ordered by department name and salary descending.

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_idINTDepartment assigned to the employee
salaryNUMERIC(10,2)Annual employee salary
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results