Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
Experiences
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
Build my plan
SQL With Complex Joins and Aggregations
00:00
5 left

SQL With Complex Joins and Aggregations

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.

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

Your question is SQL With Complex Joins and Aggregations. Start with the requirements and the four tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.