Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Three Departments Query
00:00
5 left

Top Three Departments Query

MediumSQL · PostgreSQL

Problem

Write a query to find the top three highest-paying departments using a window function.

Use average employee salary to define highest-paying. Include departments whose rank is among the top three, allowing ties to share a rank.

Output

  1. One row per qualifying department
  2. Columns: department_name, average_salary, and department_rank
  3. Sort by department_rank, then department_name

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee name
department_idINTEmployee department identifier
salaryINTAnnual employee salary
Tablesdepartmentsemployees
Interviewer

Your question is Top Three Departments Query. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.