Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Doctors by Appointment Volume

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

Your question is Top Doctors by Appointment Volume. Start with the requirements and the two 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

Practo's Business Analytics team wants to identify doctors with the highest appointment volume on the Practo platform. Write a PostgreSQL query that joins doctor and appointment data, aggregates appointments by doctor, and returns the top five doctors.

Requirements

  1. Include doctors with no appointments during aggregation, using a LEFT JOIN.
  2. Count every appointment, regardless of its status.
  3. Group results by doctor and return the doctor's ID, name, and appointment count.
  4. Sort by appointment count descending, then doctor_id ascending to break ties, and return only five doctors.

Schema

doctors
ColumnTypeDescription
doctor_idPKINTEGERUnique doctor identifier
doctor_nameVARCHAR(100)Doctor's display name
specializationVARCHAR(100)Medical specialization
appointments
ColumnTypeDescription
appointment_idPKINTEGERUnique appointment identifier
doctor_idINTEGERReferenced doctor identifier
appointment_dateDATEDate of the appointment
statusVARCHAR(20)Appointment status
Tablesdoctorsappointments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results