Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Second and Nth Highest Values
00:00
5 left

Second and Nth Highest Values

MediumSQL · PostgreSQL

Problem

How would you write a SQL query to find the second and nth highest salary in an employee table?

Treat equal salaries as one rank and exclude employees whose salary is NULL. The provided solution returns the second-highest distinct salary; the same query can be adjusted for any fixed rank.

Output

  1. One row containing the column salary
  2. Return the requested distinct salary, ordered from highest to lowest before selecting its rank

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee full name
departmentVARCHAR(50)Employee department
salaryNUMERIC(12,2)Annual employee salary
hire_dateDATEEmployee hire date
Tablesemployees
Interviewer

Your question is Second and Nth Highest Values. Start with the requirements and the one table 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.