Healthcare: Total Fees Collected Per Doctor
SQL coding challenge · Difficulty: easy · Topic: Joins · +50 XP
Problem
The billing department wants to see how much each doctor has earned in total consultation fees.
Tables
Table: Patients
| patient_id | name | age | gender | | --- | --- | --- | --- | | 1 | Raj | 35 | Male | | 2 | Mohit | 42 | Male | | 3 | Prince | 29 | Male | | 4 | Rahul | 31 | Male |
Table: Doctors
| doctor_id | name | specialization | | --- | --- | --- | | 101 | Dr. Mehta | Cardiology | | 102 | Dr. Sharma | Neurology | | 103 | Dr. Verma | Orthopedics |
Table: Appointments
| appt_id | patient_id | doctor_id | appt_date | reason | fees | | --- | --- | --- | --- | --- | --- | | 1001 | 1 | 101 | 2024-05-01 | Chest Pain | 800 | | 1002 | 2 | 102 | 2024-05-02 | Headache | 700 | | 1003 | 3 | 103 | 2024-05-03 | Knee Pain | 600 | | 1004 | 1 | 103 | 2024-05-05 | Back Pain | 600 | | 1005 | 4 | 101 | 2024-05-06 | Regular Checkup | 800 | | 1006 | 2 | 101 | 2024-05-07 | Follow Up | 800 |
Expected Output
| name | total_fees | | --- | --- | | Dr. Mehta | 2400 | | Dr. Verma | 1200 | | Dr. Sharma | 700 |
- Return:
doctor_name,total_fees - Only include doctors with at least one appointment
- Sort by
total_feesdescending
What this SQL challenge teaches you
“Healthcare: Total Fees Collected Per Doctor” is a easy-level SQL challenge focused on Joins. Working through it gives you hands-on practice with JOIN, SUM, GROUP BY, ORDER BY — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- JOIN
- SUM
- GROUP BY
- ORDER BY
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Your result should return: doctor_name, total_fees.
- Order the output total_fees descending.
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
Where this comes up
Variations of this problem have been reported in interviews at Oracle, Infosys, Microsoft. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find Customers Missing from New Table
- Customers With at Least One Order
- Customers Who Have Never Placed an Order
- Count Orders Per Customer Including Zero
- Total Amount Spent Per Customer (With Zero)
- Find Employees in the HR Department
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Joins.