SQL Exercises/The Hospital: Relational Schema
SQL Exercises/The Hospital: Relational Schema
Relational Schema
Exercises
Obtain the names of all physicians that have performed a medical procedure they have never been certified to perform.
SELECT Name
FROM Physician
WHERE EmployeeID IN
(
SELECT Physician FROM Undergoes U WHERE NOT EXISTS
(
SELECT * FROM Trained_In
WHERE Treatment = Procedure
AND Physician = U.Physician
)
);
SELECT Name
FROM Physician
WHERE EmployeeID IN
(
SELECT Undergoes.Physician
FROM Undergoes
LEFT JOIN Trained_In
ON Undergoes.Physician=Trained_In.Physician
AND Undergoes.Procedures=Trained_In.Treatment
WHERE Treatment IS NULL
);
SELECT Physician.Name
FROM Physician
INNER JOIN Undergoes ON Physician.EmployeeID = Undergoes.Physician
LEFT JOIN Trained_In ON Undergoes.Procedures = Trained_In.Treatment AND Physician.EmployeeID =
Trained_In.Physician
WHERE Trained_In.Treatment IS NULL
GROUP BY Physician.EmployeeID
Same as the previous query, but include the following information in the results: Physician name, name of procedure, date when the procedure was carried out, name
of the patient the procedure was carried out on.
Obtain the names of all physicians that have performed a medical procedure that they are certified to perform, but such that the procedure was done at a date
(Undergoes.Date) after the physician's certification expired (Trained_In.CertificationExpires).
SELECT Name
FROM Physician
WHERE EmployeeID IN
(
SELECT Physician FROM Undergoes U
WHERE Date >
(
SELECT CertificationExpires
FROM Trained_In T
WHERE T.Physician = U.Physician
AND T.Treatment = U.Procedure
)
);
Same as the previous query, but include the following information in the results: Physician name, name of procedure, date when the procedure was carried out, name
of the patient the procedure was carried out on, and date when the certification expired.
Obtain the information for appointments where a patient met with a physician other than his/her primary care physician. Show the following information: Patient
name, physician name, nurse name (if any), start and end time of appointment, examination room, and the name of the patient's primary care physician.
The Patient field in Undergoes is redundant, since we can obtain it from the Stay table. There are no constraints in force to prevent inconsistencies between these two
tables. More specifically, the Undergoes table may include a row where the patient ID does not match the one we would obtain from the Stay table through the
Undergoes.Stay foreign key. Select all rows from Undergoes that exhibit this inconsistency.
Obtain the names of all the nurses who have ever been on call for room 123.
The hospital has several examination rooms where appointments take place. Obtain the number of appointments that have taken place in each examination room.
N.b. The solution below fails in MS SQL Server Management Studio, with the following message:
Obtain the names of all patients (also include, for each patient, the name of the patient's primary care physician), such that \emph{all} the following are true:
The patient has been prescribed some medication by his/her primary care physician.
The patient has undergone a procedure with a cost larger that $5,000
The patient has had at least two appointment where the nurse who prepped the appointment was a registered nurse.
The patient's primary care physician is not the head of any department.
Click to see solution
Sample dataset
INSERT INTO Physician VALUES(1,'John Dorian','Staff Internist',111111111);
INSERT INTO Physician VALUES(2,'Elliot Reid','Attending Physician',222222222);
INSERT INTO Physician VALUES(3,'Christopher Turk','Surgical Attending Physician',333333333);
INSERT INTO Physician VALUES(4,'Percival Cox','Senior Attending Physician',444444444);
INSERT INTO Physician VALUES(5,'Bob Kelso','Head Chief of Medicine',555555555);
INSERT INTO Physician VALUES(6,'Todd Quinlan','Surgical Attending Physician',666666666);
INSERT INTO Physician VALUES(7,'John Wen','Surgical Attending Physician',777777777);
INSERT INTO Physician VALUES(8,'Keith Dudemeister','MD Resident',888888888);
INSERT INTO Physician VALUES(9,'Molly Clock','Attending Psychiatrist',999999999);
Text is available under the Creative Commons Attribution-ShareAlike License.; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.