SQL Lab Exercises 2012
SQL Lab Exercises 2012
PATIENTS
Patientno Surname Forename DOB Address
APPOINTMENTS
Appno Doctorno Patientno Date Time Room
DOCTORS
Doctorno Surname Firstname Tel Specialization
3. Add constraints date greater than today; room number range 100 and 350
ALTER TABLE Appointments ADD CONSTRAINT Date_Check CHECK (Appdate> getdate()) ALTER TABLE Appointments ADD CONSTRAINT Room_No CHECK Room BETWEEN 100 AND 350
4. Import from Northwind database the following table: Customers and use it to populate the Patients database
INSERT INTO Doctors (doctorno, surname, forename) SELECT Employeeid, Lastname, Firstname FROM Employees INSERT INTO Patients (patientno, surname, address) SELECT Customerid, lname, address FROM Customers;
Page 1 of 5
9. List the doctors who have no appointments between 3/23/2012 and 3/30/2012.
SELECT Doctors.Doctorno, doctors.surname, doctors.forename FROM Doctors, Appointments WHERE Doctors.Doctorno NOT IN (SELECT Appointments.Doctorno FROM Appointments WHERE Appdate BETWEEN '3/23/2012' AND '3/30/2012')
Page 2 of 5
13. Self join Get the list of doctors and their patients
SELECT Appointments.Doctorno, Appointments.patientno FROM Appointments, Appointments App1 WHERE Appointments.Doctorno=App1.Doctorno SELECT DISTINCT Appointments.Doctorno, Appointments.patientno FROM Appointments, Appointments App1 WHERE Appointments.Doctorno=App1.Doctorno
15. Get the average fee for appointments that have more than 4 patients.
SELECT AVG(Fee) AS [Average Fees for Appointments with More than 4 Patients] FROM Appointments GROUP BY Doctorno HAVING COUNT(Patientno)>4 SELECT COUNT(Patientno), AVG(Fee) AS [Average Fees for Appointments with More than 4 Patients] FROM Appointments GROUP BY Doctorno HAVING COUNT(Patientno)>4
16. List the doctor appointments; including those who have no patients
SELECT Doctors.Doctorno, doctors.surname, doctors.forename, Appointments.patientno FROM Doctors LEFT JOIN Appointments ON Doctors.Doctorno=Appointments.Doctorno
Page 3 of 5
18. Views CREATE VIEW [Appointmenst for Doctor no 23] AS SELECT * FROM Appointments WHERE Doctorno='23' SELECT * FROM [Appointmenst for Doctor no 23] 19. Add tables Rooms and Department CREATE TABLE Rooms (Roomno Varchar(3) PRIMARY KEY, type Varchar(20) NOT NULL CONSTRAINT room_type CHECK (type IN ('Office','Clinic', 'Theatre','Lab')), size Int CONSTRAINT room_max CHECK (size BETWEEN 1 AND 50)); CREATE TABLE Dept (deptno varchar(5) primary key, name varchar(20) not null); 20. Create relationships between Rooms and Appointment; Departments and Doctors ALTER TABLE Appointments ADD CONSTRAINT Room_ref FOREIGN KEY (Room) REFERENCES Rooms(Roomno); ALTER TABLE Doctors ADD deptid varchar(5) FOREIGN KEY REFERENCES Dept(deptno); 21. List Rooms occupied each day/List schedule of the rooms SELECT Rooms.roomno,Appointments.Doctorno,Appointments.Patientno FROM Rooms INNER JOIN Appointments ON Rooms.roomno=Appointments.room; 22. Which is the largest Clinic? 23. Which Theatre has Eye Surgeons booked? 24. What is the booking for Clinic 356 between Jan 4 and May 30? 25. Which days is dr Juma not booked for Theatre? 26. Which days are all labs free? 27. Which department has the least number of surgeons? 28. How much fee is due to dr Juma between Jan and May 2008? 29. Which patients are above 50 years?
Page 4 of 5
30. Cancel all appointments for dr Juma for June 30th 31. Produce bill for patient Ken Kimani for the month of June 2008 32. Which doctor makes the highest amount? 33. How many female and male patients were booked for theatre between Jan and June 2008? 34. Which room type did dr Juma use for appointment on 3 June 2008? 35. Add table for Medical Scheme CREATE TABLE Scheme (code varchar(4) PRIMARY KEY, name varchar(50) NOT NULL, limit money); 36. Relate table Scheme to Patients 37. List the schemes and the details of patients in the scheme. 38. Which patients have no scheme? 39. What is the average fee charged? Which doctor charges this? 40. What is the average medical limit? 41. List the Top 5 scheme in terms of limit. 42. List patients who have not made any appointment to date. 43. For each scheme, list the patient numbers, name, dates of appointment and fee due. SELECT Scheme.name, patients.patientno, patients.surname, appointments.fee FROM scheme INNER JOIN patients ON scheme.code=patients.scheme INNER JOIN Appointments ON appointments.patientno = patients.patientno; 44. For each room, list appointment number, date and number and names of patients for the month of June=2011 45. Which scheme has the largest number of patients? Which scheme has the largest number of patients booked? 46. Change the bookings for dr Juma for 23rd June 2011 at 10am 24th June 2011 at 8am. 47. All patients in the Alico scheme should now be changed to CFC. 48. What is the average number of males and females booked in 2011?
Page 5 of 5