Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Prescription Claims Gaps and Top Pharmacy
00:00
5 left

Prescription Claims Gaps and Top Pharmacy

HardSQL · PostgreSQL

Problem

OptumRx needs a member-level view of paid prescription utilization for the first quarter of 2025. Write a PostgreSQL query that combines member, prescription, claim, and pharmacy data.

Requirements

  1. Include only active members and paid claims dated from 2025-01-01 through 2025-03-31.
  2. Return each qualifying member's total paid amount, paid claim count, distinct medication count, latest paid claim date, and largest gap in days between consecutive paid claims.
  3. Identify the pharmacy with the highest paid claim spend for each member, using a window function to rank pharmacies.
  4. Keep members with at least two paid claims and total paid amount of at least 100. Treat missing pharmacy matches as Unknown and missing monetary values as zero during aggregation.
  5. Sort results by total paid amount descending and member ID ascending.

Output details

  • Return columns in this order: member_id, member_name, total_paid, paid_claim_count, distinct_medications, latest_paid_claim, max_claim_gap_days, top_pharmacy.
  • Order claims for consecutive-date calculations by claim_date ascending, then claim_id ascending.
  • Break equal pharmacy spending ties by pharmacy_name ascending.
  • Treat the first paid claim's gap as 0 days.

Schema

members
ColumnTypeDescription
member_idPKINTOptumRx member identifier
member_nameVARCHAR(100)Member display name
is_activeBOOLEANWhether the member is currently active
prescriptions
ColumnTypeDescription
prescription_idPKINTPrescription identifier
member_idINTReferences members.member_id
drug_nameVARCHAR(100)Medication name
claims
ColumnTypeDescription
claim_idPKINTPrescription claim identifier
prescription_idINTReferences prescriptions.prescription_id
pharmacy_idINTReferences pharmacies.pharmacy_id when available
claim_dateDATEDate the claim was processed
claim_statusVARCHAR(20)Claim adjudication status
paid_amountNUMERIC(10,2)Amount paid for the claim
pharmacies
ColumnTypeDescription
pharmacy_idPKINTPharmacy identifier
pharmacy_nameVARCHAR(120)Pharmacy or dispensing channel name
Tablesmembersprescriptionsclaimspharmacies
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the four tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.