Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Department High Earners (Top Three Salaries)

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

Your question is Department High Earners (Top Three 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

TCS iON workforce reporting needs a query that identifies high-earning employees within every department. An employee is a high earner when their salary belongs to one of the department's top three unique, non-NULL salary values.

Write a PostgreSQL query that returns all qualifying employees, including every employee tied at one of those salary levels.

Requirements

  1. Join employees to their department names.
  2. Rank distinct salary levels independently within each department, with the highest salary ranked first.
  3. Return all employees whose salary rank is 1, 2, or 3. Do not return employees with NULL salaries or employees without a valid department.
  4. Include departments with fewer than three salary levels by returning every available qualifying level.
  5. Order the output by department name, salary rank, salary descending, and employee ID.

Schema

departments
ColumnTypeDescription
department_idPKINTEGERUnique department identifier
department_nameVARCHAR(100)Name of the department
employees
ColumnTypeDescription
employee_idPKINTEGERUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
department_idINTEGEREmployee's department identifier
salaryNUMERIC(12,2)Annual employee salary
Tablesdepartmentsemployees
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results