0% found this document useful (0 votes)
13 views3 pages

Database Copitol

The document provides SQL queries for various tasks related to a Hospital Management System. It includes queries to find the average number of visits per doctor, list the top 3 doctors by visits, show patients with more than 5 visits, display doctors treating over 20 patients, check for patients with no visit records, and insert emergency visits into a new table. Each query is accompanied by a brief description of its purpose.

Uploaded by

osaalimgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Database Copitol

The document provides SQL queries for various tasks related to a Hospital Management System. It includes queries to find the average number of visits per doctor, list the top 3 doctors by visits, show patients with more than 5 visits, display doctors treating over 20 patients, check for patients with no visit records, and insert emergency visits into a new table. Each query is accompanied by a brief description of its purpose.

Uploaded by

osaalimgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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!

You might also like