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