Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Top Service Categories

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

Your question is Rank Top Service Categories. Start with the requirements and the three 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

ServiceTitan wants to compare service-category performance across operating regions. Write a PostgreSQL query that identifies the top three service categories in each region based on completed job revenue during January 2025.

Requirements

  1. Join the regional, service-category, and job data.
  2. Include only jobs with status = 'completed' and completion dates in January 2025.
  3. Aggregate completed invoice revenue by region and service category.
  4. Use RANK() or DENSE_RANK() to return every category whose rank is 3 or better, including ties. Sort the final output by region, revenue descending, and category name.

Schema

regions
ColumnTypeDescription
region_idPKINTEGERUnique operating region identifier
region_nameVARCHAR(100)Operating region name
service_categories
ColumnTypeDescription
category_idPKINTEGERUnique service category identifier
category_nameVARCHAR(100)ServiceTitan service category name
jobs
ColumnTypeDescription
job_idPKINTEGERUnique job identifier
region_idINTEGERReferences regions.region_id
category_idINTEGERReferences service_categories.category_id
statusVARCHAR(30)Job lifecycle status
completed_atDATEDate the job was completed
invoice_totalNUMERIC(12,2)Total invoiced amount for the job
Tablesregionsservice_categoriesjobs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results