Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top-N Per Department

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

Your question is SQL Top-N Per Department. 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

Hexaware Technologies needs a compensation report showing the three highest-paid employees within each department. Write a PostgreSQL query that ranks employees independently inside their departments.

Requirements

  1. Join employees to departments and return the department name, employee name, salary, and rank.
  2. Return no more than the top three employees per department, ordered by salary descending.
  3. Exclude employees without a salary or department assignment, and use employee_id to make salary ties deterministic.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee full name
department_idINTDepartment assigned to the employee
salaryNUMERIC(12,2)Annual employee salary
departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
locationVARCHAR(100)Primary department office location
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results