Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second Largest Salary Query

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

Your question is Second Largest Salary Query. 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

Capgemini Government Solutions needs a compensation report for employees assigned to current government delivery programs. Write a PostgreSQL query to find employees whose salary is the second-highest distinct salary among eligible employees.

Requirements

  1. Join employees to employee_assignments using employee_id.
  2. Include only employees with employment_status = 'Active' and a current assignment.
  3. Rank distinct salary levels from highest to lowest, and return every employee at rank 2. Exclude NULL salaries.
  4. Return employee_id, employee_name, and salary, ordered by salary descending and employee ID ascending.

Schema

employees
ColumnTypeDescription
employee_idPKINTEGERUnique employee identifier
employee_nameVARCHAR(100)Employee full name
salaryNUMERIC(10,2)Annual employee salary
employment_statusVARCHAR(20)Employment status
employee_assignments
ColumnTypeDescription
assignment_idPKINTEGERUnique assignment identifier
employee_idINTEGEREmployee assigned to the program
program_nameVARCHAR(120)Government delivery program name
is_currentBOOLEANWhether the assignment is currently active
Tablesemployeesemployee_assignments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results