Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Contractors by Department

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

Your question is Top Contractors by 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

Business Context

General Dynamics Information Technology needs a department-level view of contractor compensation for workforce planning and staffing reviews.

Task

Write a PostgreSQL query that returns the three highest-paid contractors within each department.

Requirements

  1. Join contractors to their department records.
  2. Exclude contractors whose annual salary is NULL.
  3. Use a ranking window function partitioned by department.
  4. Return the contractor name, department name, annual salary, and rank. Include no more than three contractors per department, with ties resolved deterministically by contractor name.
  5. Order the final output by department name and rank.

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
contractors
ColumnTypeDescription
contractor_idPKINTUnique contractor identifier
department_idINTAssigned department identifier
contractor_nameVARCHAR(100)Contractor's name
annual_salaryNUMERIC(12,2)Annual contractor salary
Tablesdepartmentscontractors
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results