Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second and Nth Highest Values

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results