Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

UST SQL: Second Maximum Salary

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

Your question is UST SQL: 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

UST's payroll reporting team needs to identify employees earning the second-highest distinct salary. Write a PostgreSQL query that returns every employee tied at that salary and includes the department name when available.

Requirements

  1. Rank distinct salary values from highest to lowest, ignoring ties when determining the second-highest value.
  2. Return the employee ID, employee name, salary, and department name for every employee at rank two.
  3. Preserve qualifying employees even when their department is missing, and sort results by salary descending and employee ID ascending.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee 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