In this scenario, we will create SQL queries to analyze healthcare data involving patients, doctors, appointments, and treatments. We will use various types of SQL joins to derive insights from the data, such as patient treatment history, doctor performance metrics, and appointment schedules.
| patient_id | name | dob | gender | referred_by |
|---|---|---|---|---|
| 1 | Alice | 1980-01-01 | F | NULL |
| 2 | Bob | 1975-05-21 | M | 1 |
| 3 | Charlie | 1990-07-15 | M | NULL |
| 4 | Diana | 1985-03-10 | F | 2 |
| doctor_id | name | specialty | hire_date |
|---|---|---|---|
| 1 | Dr. Smith | Cardiology | 2010-06-01 |
| 2 | Dr. Jones | Dermatology | 2012-09-15 |
| 3 | Dr. Brown | General Surgery | 2015-11-20 |
| appointment_id | patient_id | doctor_id | appointment_date | status |
|---|---|---|---|---|
| 1 | 1 | 1 | 2024-06-01 09:00:00 | Completed |
| 2 | 2 | 2 | 2024-06-02 10:00:00 | Scheduled |
| 3 | 3 | 3 | 2024-06-03 11:00:00 | Completed |
| 4 | 1 | 3 | 2024-06-04 12:00:00 | Scheduled |
| treatment_id | appointment_id | treatment_type | cost |
|---|---|---|---|
| 1 | 1 | ECG | 200 |
| 2 | 3 | Surgery | 5000 |
| 3 | 4 | Consultation | 150 |
This query retrieves the complete treatment history for each patient, including details of the doctor who administered the treatment.
SELECT
p.patient_id,
p.name AS patient_name,
d.name AS doctor_name,
a.appointment_date,
t.treatment_type,
t.cost
FROM
patients p
JOIN
appointments a ON p.patient_id = a.patient_id
JOIN
doctors d ON a.doctor_id = d.doctor_id
JOIN
treatments t ON a.appointment_id = t.appointment_id
ORDER BY
p.patient_id, a.appointment_date;
Output:
| patient_id | patient_name | doctor_name | appointment_date | treatment_type | cost |
|---|---|---|---|---|---|
| 1 | Alice | Dr. Smith | 2024-06-01 09:00:00 | ECG | 200 |
| 1 | Alice | Dr. Brown | 2024-06-04 12:00:00 | Consultation | 150 |
| 3 | Charlie | Dr. Brown | 2024-06-03 11:00:00 | Surgery | 5000 |
This query lists all patients and their upcoming appointments, if any. Patients without upcoming appointments will also be included in the results.
SELECT
p.patient_id,
p.name AS patient_name,
a.appointment_date,
d.name AS doctor_name,
a.status
FROM
patients p
LEFT JOIN
appointments a ON p.patient_id = a.patient_id AND a.status = 'Scheduled'
LEFT JOIN
doctors d ON a.doctor_id = d.doctor_id
WHERE
a.appointment_date >= CURRENT_DATE
ORDER BY
p.patient_id, a.appointment_date;
Output:
| patient_id | patient_name | appointment_date | doctor_name | status |
|---|---|---|---|---|
| 1 | Alice | 2024-06-04 12:00:00 | Dr. Brown | Scheduled |
| 2 | Bob | 2024-06-02 10:00:00 | Dr. Jones | Scheduled |
| 3 | Charlie | NULL | NULL | NULL |
| 4 | Diana | NULL | NULL | NULL |
This query provides performance metrics for each doctor, including the number of appointments they have handled and the total cost of treatments they have administered. Doctors with no appointments will also be included.
SELECT
d.doctor_id,
d.name AS doctor_name,
d.specialty,
COUNT(a.appointment_id) AS total_appointments,
SUM(t.cost) AS total_treatment_cost
FROM
doctors d
RIGHT JOIN
appointments a ON d.doctor_id = a.doctor_id
LEFT JOIN
treatments t ON a.appointment_id = t.appointment_id
GROUP BY
d.doctor_id, d.name, d.specialty
ORDER BY
total_appointments DESC;
SELECT
d.doctor_id,
d.name AS doctor_name,
d.specialty,
COUNT(a.appointment_id) AS total_appointments,
SUM(t.cost) AS total_treatment_cost
FROM
doctors d
RIGHT JOIN
appointments a ON d.doctor_id = a.doctor_id
LEFT JOIN
treatments t ON a.appointment_id = t.appointment_id
GROUP BY
d.doctor_id, d.name, d.specialty
ORDER BY
total_appointments DESC;
Output:
| doctor_id | doctor_name | specialty | total_appointments | total_treatment_cost |
|---|---|---|---|---|
| 3 | Dr. Brown | General Surgery | 2 | 5150 |
| 1 | Dr. Smith | Cardiology | 1 | 200 |
| 2 | Dr. Jones | Dermatology | 1 | NULL |
This query creates a comprehensive audit report by listing all appointments and their corresponding treatments, if any. Both appointments without treatments and treatments without appointments will be included.
SELECT
a.appointment_id,
a.appointment_date,
a.status,
p.name AS patient_name,
d.name AS doctor_name,
t.treatment_type,
t.cost
FROM
appointments a
FULL OUTER JOIN
treatments t ON a.appointment_id = t.appointment_id
LEFT JOIN
patients p ON a.patient_id = p.patient_id
LEFT JOIN
doctors d ON a.doctor_id = d.doctor_id
ORDER BY
a.appointment_date;
Output:
| appointment_id | appointment_date | status | patient_name | doctor_name | treatment_type | cost |
|---|---|---|---|---|---|---|
| 1 | 2024-06-01 09:00:00 | Completed | Alice | Dr. Smith | ECG | 200 |
| 2 | 2024-06-02 10:00:00 | Scheduled | Bob | Dr. Jones | NULL | NULL |
| 3 | 2024-06-03 11:00:00 | Completed | Charlie | Dr. Brown | Surgery | 5000 |
| 4 | 2024-06-04 12:00:00 | Scheduled | Alice | Dr. Brown | Consultation | 150 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL |
This query identifies patients who have been referred by other patients. The referred_by field in the patients table indicates which patient referred them.
SELECT
p1.patient_id AS patient_id,
p1.name AS patient_name,
p2.patient_id AS referred_by_id,
p2.name AS referred_by_name
FROM
patients p1
JOIN
patients p2 ON p1.referred_by = p2.patient_id
ORDER BY
p1.patient_id;
Output:
| patient_id | patient_name | referred_by_id | referred_by_name |
|---|---|---|---|
| 2 | Bob | 1 | Alice |
| 4 | Diana | 2 | Bob |