Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Rank Per Department

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

Your question is SQL Rank Per Department. 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

Infocepts Workforce Analytics needs a department-level salary benchmark. Write a PostgreSQL query to return the second-highest distinct salary for every department.

Requirements

  1. Use DENSE_RANK() or an equivalent ranking approach so tied salaries share the same rank.
  2. Exclude employees whose salary is NULL before ranking.
  3. Return every department, including departments without a second distinct salary, with NULL in that result column.
  4. Order the output by department 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_idINTDepartment assigned to the employee
salaryNUMERIC(12,2)Annual employee salary
Tablesdepartmentsemployees
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results