Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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 Join and Query Building
00:00
5 left

SQL Join and Query Building

HardSQL · PostgreSQL

Problem

Evernorth runs pharmacy benefits for millions of members, and a care-management analyst needs a report joining member, provider, and prescription-fill data. You are given the schema below and asked to write the query from scratch.

CREATE TABLE members (
    member_id INT PRIMARY KEY,
    member_name VARCHAR(120),
    state CHAR(2),
    plan_type VARCHAR(30)
);

CREATE TABLE providers (
    provider_id INT PRIMARY KEY,
    provider_name VARCHAR(120),
    specialty VARCHAR(60)
);

CREATE TABLE prescriptions (
    prescription_id INT PRIMARY KEY,
    member_id INT REFERENCES members(member_id),
    provider_id INT REFERENCES providers(provider_id),
    drug_name VARCHAR(120),
    fill_date DATE,
    cost NUMERIC(8,2)
);

Write a single SQL query that returns, for every member with more than 3 prescription fills in the last 90 days, the member's name, the names of every provider who prescribed to them in that window, and their total prescription cost, sorted by total cost descending. Explain your join and grouping choices out loud.

Output

  1. One row per qualifying member.
  2. Columns: member_name, provider_names, and total_prescription_cost.
  3. Include only members with more than 3 fills in the last 90 days.
  4. Sort by total_prescription_cost descending, then member_name ascending for ties.

Schema

members
ColumnTypeDescription
member_idPKINTUnique Evernorth member identifier
member_nameVARCHAR(120)Member's full name
stateCHAR(2)Two-letter member state code
plan_typeVARCHAR(30)Member benefit plan type
providers
ColumnTypeDescription
provider_idPKINTUnique provider identifier
provider_nameVARCHAR(120)Provider's full name
specialtyVARCHAR(60)Provider medical specialty
prescriptions
ColumnTypeDescription
prescription_idPKINTUnique prescription fill identifier
member_idINTReferenced member identifier
provider_idINTReferenced prescribing provider identifier
drug_nameVARCHAR(120)Dispensed drug name
fill_dateDATEPrescription fill date
costNUMERIC(8,2)Prescription cost
Tablesmembersprovidersprescriptions
Interviewer

Your question is SQL Join and Query Building. Start with the requirements and the three 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.