Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Digital Services Per Region

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

Your question is Top Digital Services Per Region. 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

Apple wants to compare the monthly performance of digital services such as Apple Music, Apple TV+, and iCloud+ across customer regions. Write a PostgreSQL query that aggregates service revenue by month and identifies the highest-performing services within each region and month.

Requirements

  1. Join service_usage to digital_services and aggregate revenue by region, month, and service.
  2. Use DENSE_RANK to rank services by monthly revenue within each region and month, preserving ties.
  3. Use ROW_NUMBER to provide a deterministic position within each region and month, ordering tied services by service name.
  4. Return services in the top two dense-rank levels, ordered by region, month, and row number. Treat missing revenue as zero.

Schema

digital_services
ColumnTypeDescription
service_idPKINTUnique Apple digital service identifier
service_nameVARCHAR(100)Apple digital service name
service_categoryVARCHAR(50)Category of the digital service
service_usage
ColumnTypeDescription
usage_idPKINTUnique usage record identifier
service_idINTReferences digital_services.service_id
regionVARCHAR(50)Customer geographic region
usage_dateDATEDate associated with usage revenue
revenueNUMERIC(12,2)Revenue attributed to the service usage
Tablesdigital_servicesservice_usage
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results