Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second Highest Tier Revenue

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

Your question is Second Highest Tier Revenue. 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

Paramount+ analysts need to compare subscription-tier revenue across geographic markets. Write a PostgreSQL query that finds the second-highest revenue-generating tier within each region.

Requirements

  1. Include only rows with status = 'paid' and a non-null revenue value.
  2. Aggregate revenue by region and subscription tier.
  3. Use a window function to rank distinct revenue levels within each region, retaining ties correctly.
  4. Return the region, tier name, and total revenue for rank 2, ordered alphabetically by region.

Regions with fewer than two qualifying tiers should not appear in the result.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique geographic region identifier
region_nameVARCHAR(100)Display name of the geographic region
plans
ColumnTypeDescription
plan_idPKINTUnique Paramount+ plan identifier
tier_nameVARCHAR(50)Name of the subscription tier
subscriptions
ColumnTypeDescription
subscription_idPKINTUnique billing record identifier
region_idINTRegion associated with the subscription
plan_idINTPlan associated with the subscription
revenueNUMERIC(10,2)Revenue recorded for the billing record
statusVARCHAR(20)Billing status, such as paid or pending
Tablesregionsplanssubscriptions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results