Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Meds Per Region SQL

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

Your question is Top Meds Per Region SQL. 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

Aetna wants a regional view of medication utilization from its claims data. Write a PostgreSQL query that identifies the three most prescribed medications in each region during the first quarter of 2025.

Requirements

  1. Include only prescriptions with claim_status = 'Approved' and dates from January 1 through March 31, 2025.
  2. Join prescriptions to Aetna's medication and region reference tables.
  3. Count prescriptions for each medication within each region, then rank medications by count descending within the region using a window function.
  4. Return only ranks 1 through 3, including ties resolved by medication name alphabetically.

Schema

prescriptions
ColumnTypeDescription
prescription_idPKINTUnique prescription claim identifier
region_idINTReferences the service region
medication_idINTReferences the prescribed medication
prescribed_dateDATEDate on which the medication was prescribed
claim_statusVARCHAR(20)Claim processing status
medications
ColumnTypeDescription
medication_idPKINTUnique medication identifier
medication_nameVARCHAR(100)Generic or brand medication name
regions
ColumnTypeDescription
region_idPKINTUnique service region identifier
region_nameVARCHAR(50)Aetna service region name
Tablesprescriptionsmedicationsregions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results