Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Query for Third Largest

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

Your question is SQL Query for Third Largest. 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

Sun Life Financial's QA team needs to validate salary-ranking logic in an employee reporting query. Write a PostgreSQL query that identifies employees earning the third-highest distinct salary among active employees with recorded compensation.

Requirements

  1. Join employees to employee_compensation using employee_id.
  2. Exclude inactive employees and rows where salary is NULL.
  3. Rank distinct salary values from highest to lowest and return every employee tied at rank 3.
  4. Order the results by salary descending and employee ID ascending.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee full name
employment_statusVARCHAR(20)Current employment status
departmentVARCHAR(50)Employee department
employee_compensation
ColumnTypeDescription
compensation_idPKINTUnique compensation record identifier
employee_idINTEmployee receiving the compensation
salaryNUMERIC(12,2)Annual base salary
Tablesemployeesemployee_compensation
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results