Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Top Active Doctors by Specialty

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

Your question is SQL: Top Active Doctors by Specialty. 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

Doximity wants to identify the most active doctors in each medical specialty based on activity recorded in the Doximity platform. Assume the evaluation date is 2025-02-15.

Write a PostgreSQL query to return the top three doctors in every specialty based on the number of activity events during the 30-day period ending on the evaluation date.

Requirements

  1. Include doctors with zero activity during the period, if they are assigned to a specialty.
  2. Count only events from 2025-01-17 through 2025-02-15, inclusive.
  3. Rank doctors independently within each specialty by activity count descending.
  4. Break ties deterministically using doctor_id ascending and return no more than three doctors per specialty.
  5. Return specialty, doctor, activity count, and rank, ordered by specialty and rank.

Schema

doctors
ColumnTypeDescription
doctor_idPKINTUnique Doximity doctor identifier
full_nameVARCHAR(150)Doctor's full name
specialties
ColumnTypeDescription
specialty_idPKINTUnique medical specialty identifier
specialty_nameVARCHAR(100)Medical specialty name
doctor_specialties
ColumnTypeDescription
doctor_idINTReferences doctors.doctor_id
specialty_idINTReferences specialties.specialty_id
activity_events
ColumnTypeDescription
event_idPKINTUnique activity event identifier
doctor_idINTReferences doctors.doctor_id; null for unattributed activity
occurred_atTIMESTAMPTimestamp when the activity occurred
event_typeVARCHAR(50)Type of Doximity activity
Tablesdoctorsspecialtiesdoctor_specialtiesactivity_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results