Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Rows With SQL Window Functions

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

Your question is Rank Rows With SQL Window Functions. 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

Tech Mahindra's workforce analytics team needs to compare active employees within their departments. Write a PostgreSQL query that ranks employees by salary within each department.

Requirements

  1. Join employees to departments and include only employees with employment_status = 'Active'.
  2. Use RANK() to rank salaries from highest to lowest independently within each department. Treat missing salaries as the lowest values.
  3. Return department name, employee name, salary, and salary rank, ordered by department name, rank, and employee name.

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
department_idINTEmployee's department identifier
employee_nameVARCHAR(100)Employee's full name
salaryINTAnnual salary
employment_statusVARCHAR(20)Current employment status
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results