Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Prescribers SQL by State
00:00
5 left

Top Prescribers SQL by State

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the top three prescribers for a specific medication in each state, partitioned by year.

Use the provided prescription, prescriber, and medication data. For this exercise, measure prescriber activity by prescription count for Ozempic. Exclude records without a known state and return no more than three prescribers per state and year.

Output

  1. One row per qualifying prescriber, state, and year
  2. Columns: year, state, prescriber_id, prescriber_name, prescription_count, and prescriber_rank
  3. Sort by year, state, prescriber_rank, and prescriber_id

Schema

prescriptions
ColumnTypeDescription
prescription_idPKINTUnique prescription record identifier
prescriber_idINTPrescriber who issued the prescription
medication_idINTMedication associated with the prescription
prescribed_atDATEDate the prescription was issued
prescribers
ColumnTypeDescription
prescriber_idPKINTUnique prescriber identifier
prescriber_nameVARCHAR(100)Prescriber's full name
stateVARCHAR(2)US state associated with the prescriber
medications
ColumnTypeDescription
medication_idPKINTUnique medication identifier
medication_nameVARCHAR(100)Medication product name
Tablesprescriptionsprescribersmedications
Interviewer

Your question is Top Prescribers SQL by State. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.