Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second Maximum Salary

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

Your question is Second Maximum Salary. 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

Afiniti's workforce analytics team needs to identify employees whose salary is the second-highest distinct salary across the company. Write a PostgreSQL query that returns each matching employee with their department when available.

Requirements

  1. Rank distinct salary levels from highest to lowest, excluding NULL salaries.
  2. Return every employee tied at the second-highest salary, not just one employee.
  3. Preserve employees even when their department is missing or unmatched by using a LEFT JOIN.
  4. Order the results by salary descending and employee ID ascending.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
salaryNUMERIC(12,2)Annual employee salary
department_idINTReferences departments.department_id
departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results