In this project you will design and query a small database for a clinic in Dhaka. It stores doctors, patients and appointments, and answers the questions a clinic manager asks every week: which doctor earns the most, who misses appointments, who is coming tomorrow, and is anyone double-booked? Along the way you will use joins, conditional counts, date maths, self joins, GROUP_CONCAT and NOT EXISTS together.
In this project you will
- Design three related tables with keys and
CHECKrules - Report income and visits per doctor
- Calculate a fair no-show rate
- List upcoming appointments with patient age
- Detect clashing appointments and find patients with no visits
This project uses ideas from joins, ratios, date range overlaps and NOT EXISTS.
The Design
- doctors: one row per doctor, with a specialty and a consultation fee.
- patients: one row per patient, with a birth date and an optional phone.
- appointments: links one doctor to one patient at a time, with a length in minutes and a status. A
CHECKallows only four statuses.
CREATE TABLE doctors (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
specialty TEXT NOT NULL,
fee INTEGER NOT NULL CHECK (fee > 0)
);
CREATE TABLE patients (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
born TEXT NOT NULL,
phone TEXT
);
CREATE TABLE appointments (
id INTEGER PRIMARY KEY,
doctor_id INTEGER NOT NULL REFERENCES doctors(id),
patient_id INTEGER NOT NULL REFERENCES patients(id),
starts_at TEXT NOT NULL, -- 'YYYY-MM-DD HH:MM'
minutes INTEGER NOT NULL DEFAULT 15,
status TEXT NOT NULL CHECK (status IN ('booked', 'done', 'no_show', 'cancelled'))
);
INSERT INTO doctors VALUES
(1, 'Dr. Farhana Rahman', 'Medicine', 1000),
(2, 'Dr. Mahbub Alam', 'Cardiology', 1500),
(3, 'Dr. Shirin Akter', 'Paediatrics', 800);
INSERT INTO patients VALUES
(1, 'Abdul Karim', '1958-04-12', '01711000101'),
(2, 'Rehana Begum', '1990-09-30', '01811000102'),
(3, 'Ayaan Hasan', '2020-02-14', '01911000103'),
(4, 'Selina Parvin','1975-12-01', NULL),
(5, 'Tanvir Ahmed', '2002-06-21', '01611000105');
INSERT INTO appointments VALUES
(1, 1, 1, '2026-11-20 10:00', 15, 'done'),
(2, 1, 2, '2026-11-20 10:15', 15, 'done'),
(3, 2, 1, '2026-11-20 11:00', 30, 'done'),
(4, 3, 3, '2026-11-21 09:00', 20, 'no_show'),
(5, 1, 4, '2026-11-21 10:00', 15, 'cancelled'),
(6, 2, 4, '2026-11-22 12:00', 30, 'done'),
(7, 1, 5, '2026-11-22 10:00', 15, 'no_show'),
(8, 3, 3, '2026-11-23 09:00', 20, 'done'),
(9, 2, 5, '2026-12-01 11:00', 30, 'booked'),
(10, 2, 2, '2026-12-01 11:15', 30, 'booked'),
(11, 1, 1, '2026-12-02 10:00', 15, 'booked');
Income and No-Shows
-- 1. Completed visits and fee income for each doctor
SELECT d.name, d.specialty,
COUNT(a.id) AS visits_done,
COUNT(a.id) * d.fee AS income
FROM doctors AS d
LEFT JOIN appointments AS a
ON a.doctor_id = d.id AND a.status = 'done'
GROUP BY d.id
ORDER BY income DESC;
-- 2. No-show rate per doctor, among appointments that were not cancelled
SELECT d.name,
SUM(a.status = 'no_show') AS no_shows,
COUNT(*) AS kept_or_missed,
ROUND(100.0 * SUM(a.status = 'no_show') / COUNT(*), 1) AS no_show_pct
FROM appointments AS a
JOIN doctors AS d ON d.id = a.doctor_id
WHERE a.status IN ('done', 'no_show')
GROUP BY d.id
ORDER BY no_show_pct DESC;
Output
+--------------------+-------------+-------------+--------+
| name | specialty | visits_done | income |
+--------------------+-------------+-------------+--------+
| Dr. Mahbub Alam | Cardiology | 2 | 3000 |
| Dr. Farhana Rahman | Medicine | 2 | 2000 |
| Dr. Shirin Akter | Paediatrics | 1 | 800 |
+--------------------+-------------+-------------+--------+
(3 rows)
+--------------------+----------+----------------+-------------+
| name | no_shows | kept_or_missed | no_show_pct |
+--------------------+----------+----------------+-------------+
| Dr. Shirin Akter | 1 | 2 | 50.0 |
| Dr. Farhana Rahman | 1 | 3 | 33.3 |
| Dr. Mahbub Alam | 0 | 2 | 0.0 |
+--------------------+----------+----------------+-------------+
(3 rows)
- Query 1 puts the status filter inside the
LEFT JOIN … ON, so a doctor with no completed visits would still appear with 0. Income is visits × fee. - Query 2 leaves out cancelled appointments, because a cancellation made in advance is not a no-show. Dr. Shirin Akter has the highest rate, 1 of 2 (50%), but with such small numbers it is too early to draw conclusions.
Upcoming Appointments and Clashes
-- 3. Upcoming appointments with patient age on the day
SELECT a.starts_at, d.name AS doctor, p.name AS patient,
CAST((julianday(a.starts_at) - julianday(p.born)) / 365.25 AS INTEGER) AS age,
COALESCE(p.phone, 'no phone') AS phone
FROM appointments AS a
JOIN doctors AS d ON d.id = a.doctor_id
JOIN patients AS p ON p.id = a.patient_id
WHERE a.status = 'booked'
ORDER BY a.starts_at;
-- 4. Clashes: the same doctor booked for two overlapping slots
SELECT d.name AS doctor,
a.starts_at AS first_start, a.minutes AS first_minutes,
b.starts_at AS second_start
FROM appointments AS a
JOIN appointments AS b
ON a.doctor_id = b.doctor_id
AND a.id < b.id
AND a.status = 'booked' AND b.status = 'booked'
AND b.starts_at < datetime(a.starts_at, '+' || a.minutes || ' minutes')
AND a.starts_at < datetime(b.starts_at, '+' || b.minutes || ' minutes')
JOIN doctors AS d ON d.id = a.doctor_id;
Output
+------------------+--------------------+--------------+-----+-------------+
| starts_at | doctor | patient | age | phone |
+------------------+--------------------+--------------+-----+-------------+
| 2026-12-01 11:00 | Dr. Mahbub Alam | Tanvir Ahmed | 24 | 01611000105 |
| 2026-12-01 11:15 | Dr. Mahbub Alam | Rehana Begum | 36 | 01811000102 |
| 2026-12-02 10:00 | Dr. Farhana Rahman | Abdul Karim | 68 | 01711000101 |
+------------------+--------------------+--------------+-----+-------------+
(3 rows)
+-----------------+------------------+---------------+------------------+
| doctor | first_start | first_minutes | second_start |
+-----------------+------------------+---------------+------------------+
| Dr. Mahbub Alam | 2026-12-01 11:00 | 30 | 2026-12-01 11:15 |
+-----------------+------------------+---------------+------------------+
(1 row)
- Query 3 works out each patient’s age on the day of the visit from their birth date with
julianday. Dividing by 365.25 allows for leap years, andCAST(… AS INTEGER)drops the fraction. COALESCE(p.phone, 'no phone')shows a clear message for a missing number, so the receptionist knows not to call.- Query 4 is a self join on the same doctor with the overlap rule. Each slot ends at its start time plus its length, using
datetime(start, '+30 minutes'). Dr. Mahbub Alam has a 30-minute slot at 11:00 and another at 11:15. That is a clash the clinic must fix.
Patient Questions
-- 5. Patients seen by more than one specialty
SELECT p.name,
COUNT(DISTINCT d.specialty) AS specialties,
GROUP_CONCAT(DISTINCT d.specialty) AS seen_by
FROM appointments AS a
JOIN patients AS p ON p.id = a.patient_id
JOIN doctors AS d ON d.id = a.doctor_id
WHERE a.status = 'done'
GROUP BY p.id
HAVING COUNT(DISTINCT d.specialty) > 1;
-- 6. Patients with no visit at all yet
SELECT p.name
FROM patients AS p
WHERE NOT EXISTS (
SELECT 1 FROM appointments AS a
WHERE a.patient_id = p.id AND a.status = 'done'
);
Output
+-------------+-------------+---------------------+
| name | specialties | seen_by |
+-------------+-------------+---------------------+
| Abdul Karim | 2 | Medicine,Cardiology |
+-------------+-------------+---------------------+
(1 row)
+--------------+
| name |
+--------------+
| Tanvir Ahmed |
+--------------+
(1 row)
- Query 5 uses
COUNT(DISTINCT d.specialty)andGROUP_CONCAT(DISTINCT …)to show patients seen by more than one kind of doctor. Abdul Karim saw both Medicine and Cardiology. - Query 6 uses
NOT EXISTSto find patients with no completed visit. Tanvir Ahmed missed his only past appointment and has one booked.
Ideas to Extend the Project
- Add a
prescriptionstable linked to appointments. - Add
deleted_atto patients for soft delete. - Show monthly income for each doctor with zero months filled in, using a calendar cross join.
- Add a trigger that rejects a new booking if it clashes with an existing one.
Common Mistakes
- Putting the status filter in
WHEREafter aLEFT JOIN. Doctors with no matching rows would vanish. - Counting cancellations as no-shows. Decide clearly what the denominator is.
- Storing age instead of birth date. Age changes every year; birth date does not.
- Checking clashes only on equal start times. Slots that start at different times can still overlap.
Practice Exercises
Exercise 1. Count the appointments of each status.
Show answer
SELECT status, COUNT(*) AS appointments
FROM appointments
GROUP BY status
ORDER BY appointments DESC;
done 5, booked 3, no_show 2, cancelled 1.
Exercise 2. Which patients have no phone number, and how many appointments do they have?
Show answer
SELECT p.name, COUNT(a.id) AS appointments
FROM patients AS p
LEFT JOIN appointments AS a ON a.patient_id = p.id
WHERE p.phone IS NULL
GROUP BY p.id;
Selina Parvin, with 2 appointments.
Frequently Asked Questions
How do I design a database for hospital appointments?
Use separate tables for doctors, patients and appointments. Each appointment row stores a doctor ID and a patient ID as foreign keys, plus the start time, length and status. This avoids repeating doctor and patient details on every booking.
How can SQL detect double-booked appointments?
Join the appointments table to itself on the same doctor, keep each pair once with a.id < b.id, and check that each appointment starts before the other one ends. Any returned pair is a clash.
Summary
- Three linked tables with keys and CHECK rules model a clinic cleanly.
- Filters inside
LEFT JOIN … ONkeep rows with zero matches. - Date functions give age and slot end times; the overlap rule finds clashes.
COUNT(DISTINCT),GROUP_CONCATandNOT EXISTSanswer patient questions.
Back to the start of the series: SQL SELECT Statement.
