Absolutely!
Let's tackle each of the SQL-related questions from your
document about the Hospital Management System. Here's how you could
approach them:
🩺 1. Find the average number of visits per doctor
SELECT
AVG(VisitCount) AS AvgVisitsPerDoctor
FROM (
SELECT
DoctorID,
COUNT(*) AS VisitCount
FROM Visits
GROUP BY DoctorID
) AS DoctorVisits;
🥇 2. List the top 3 doctors with the most visits
SELECT
d.DoctorID,
d.Name,
COUNT(v.VisitID) AS TotalVisits
FROM Doctors d
JOIN Visits v ON d.DoctorID = v.DoctorID
GROUP BY d.DoctorID, d.Name
ORDER BY TotalVisits DESC
LIMIT 3;
👥 3. Show patients who visited more than 5 times
SELECT
p.PatientID,
p.Name,
COUNT(v.VisitID) AS VisitCount
FROM Patients p
JOIN Visits v ON p.PatientID = v.PatientID
GROUP BY p.PatientID, p.Name
HAVING COUNT(v.VisitID) > 5;
🧑⚕️4. Display doctors who treated more than 20 patients
SELECT
d.DoctorID,
d.Name,
COUNT(DISTINCT v.PatientID) AS PatientCount
FROM Doctors d
JOIN Visits v ON d.DoctorID = v.DoctorID
GROUP BY d.DoctorID, d.Name
HAVING COUNT(DISTINCT v.PatientID) > 20;
❌ 5. Check if there exists a patient with no visit record
SELECT
p.PatientID,
p.Name
FROM Patients p
LEFT JOIN Visits v ON p.PatientID = v.PatientID
WHERE v.VisitID IS NULL;
If this query returns rows, those are patients with no visit records.
🚨 6. Insert all emergency visits into a new table EmergencyArchive
First, create the new table:
CREATE TABLE EmergencyArchive AS
SELECT * FROM Visits WHERE 1=0;
Then insert emergency visits:
INSERT INTO EmergencyArchive
SELECT * FROM Visits
WHERE Diagnosis = 'Emergency';
Let me know if you'd like help turning these into a PowerPoint, adding sample
data, or visualizing the results!