Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
UST SQL: Second Maximum Salary
00:00
5 left

UST SQL: Second Maximum Salary

EasySQL · PostgreSQL

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
Interviewer

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