Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL With Complex Joins and Aggregations

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

Your question is SQL With Complex Joins and Aggregations. Start with the requirements and the four 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

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.

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results