Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Department-Wise Second Highest Salary
00:00
5 left

Department-Wise Second Highest Salary

MediumSQL · PostgreSQL

Problem

Incedo's workforce analytics team needs to identify the second-highest salary in each department. Write a PostgreSQL query using department and employee data.

Requirements

  1. Return every employee whose salary is the second-highest distinct salary in their department.
  2. Treat tied salaries as the same salary rank and return all employees sharing rank 2.
  3. Exclude employees whose salary is NULL and departments with fewer than two distinct non-null salaries.
  4. Return results ordered by department name and employee 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_idINTReferences departments.department_id
salaryNUMERIC(12,2)Annual salary
Tablesdepartmentsemployees
Interviewer

Your question is Department-Wise Second Highest Salary. 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.