Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

N-th Highest Salary Query

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

Your question is N-th Highest Salary Query. 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

CLARA Analytics wants to benchmark compensation across its active analytics teams. Write a PostgreSQL query that returns every active employee earning the third-highest distinct salary among eligible employees.

Requirements

  1. Consider only employees with employment_status = 'Active', assigned to an active team, and with a non-NULL salary.
  2. Rank distinct salary values from highest to lowest using a window function.
  3. Return the employee ID, employee name, salary, and salary rank for the third-highest salary. Include all employees tied at that salary and sort by employee ID.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee full name
team_idINTReferenced team identifier
salaryNUMERIC(10,2)Annual employee salary
employment_statusVARCHAR(20)Current employment status
teams
ColumnTypeDescription
team_idPKINTUnique team identifier
team_nameVARCHAR(100)Team name
practiceVARCHAR(50)Analytics practice supported by the team
is_activeBOOLEANWhether the team is active
Tablesemployeesteams
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results