Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second-Last Salary Employee

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

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

Quantiphi's employee analytics team needs to identify employees whose current salary is the second-highest distinct salary across the organization. Write a PostgreSQL query that returns every employee tied at that salary and includes the department name when available.

Requirements

  1. Exclude employees whose salary is NULL from salary ranking.
  2. Rank distinct salary levels from highest to lowest, preserving ties.
  3. Return every employee with rank 2, including employee ID, name, salary, and department name.
  4. Use a LEFT JOIN so employees remain eligible even if their department is missing or unknown, and order results by salary descending and employee ID ascending.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
first_nameVARCHAR(50)Employee first name
last_nameVARCHAR(50)Employee last name
salaryNUMERIC(12,2)Current annual salary
department_idINTOptional reference to the employee department
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