Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Second Highest Salary

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

Your question is SQL Second Highest Salary. Start with the requirements and the one table 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

S&P Global needs a compensation validation query for employee records used in internal QA reporting. Write a PostgreSQL query to return the second distinct highest salary from the employees table without using analytical or window functions.

Requirements

  1. Return one row with the alias second_highest_salary.
  2. Treat duplicate salary values as one salary rank.
  3. Ignore NULL salaries and return NULL if fewer than two distinct non-NULL salaries exist.
  4. Do not use RANK, DENSE_RANK, ROW_NUMBER, or other window functions.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee name
departmentVARCHAR(50)Organizational department
salaryNUMERIC(12,2)Annual salary
Tablesemployees
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results