Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Second-Highest Paid Per Department
00:00
5 left

Second-Highest Paid Per Department

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the second-highest-paid employee within each department.

Use the provided employees and departments tables. Treat the second-highest salary as the second distinct salary, and return every employee tied at that salary.

Output

  1. One row per qualifying employee, with department_id, department_name, employee_id, employee_name, and salary
  2. Exclude employees without a valid department or salary
  3. Order by department_id, then employee_id

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee full name
department_idINTEmployee department reference
salaryNUMERIC(12,2)Annual employee salary
Tablesemployeesdepartments
Interviewer

Your question is Second-Highest Paid Per Department. 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.