Sie sind auf Seite 1von 5

SQL Lab Exercises

PATIENTS
Patientno Surname Forename DOB Address

APPOINTMENTS
Appno Doctorno Patientno Date Time Room

DOCTORS
Doctorno Surname Firstname Tel Specialization

1. Create the three tables


CREATE TABLE Patients (Patientno Varchar(10) NOT NULL, Surname Varchar(12), Forename Varchar(25), DOB Datetime, Address Varchar (12)); ALTER TABLE Patients ADD CONSTRAINT pk10 PRIMARY KEY (Patientno); CREATE TABLE Doctors (Doctorno char(5) PRIMARY KEY, Surname Varchar(12), Firstname Varchar(20), Tel Varchar(12), Specialization varchar(16)); CREATE TABLE Appointments (Appno Char(5) PRIMARY KEY, Doctorno Char(5) REFERENCES Doctors(Doctorno), Patientno VarChar(8) REFERENCES Patients(Patientno), AppDate Datetime, AppTime DateTime, Room numeric(3));

2. Add column Fee in Table Appointment


ALTER TABLE Appointments ADD Fee Money ALTER TABLE Appointments ADD COLUMN Fee Money

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

5. Simple Lists e.g. List all surgeons


SELECT * FROM Doctors WHERE Specialization='Surgeon'

List any type surgeon


SELECT * FROM Doctors WHERE Specialization LIKE '%Surgeon%'

6. List appointments for Dr Kimani (890)


SELECT * FROM Appointments WHERE doctorno='890'; SELECT Doctors.Doctorno, doctors.surname, doctors.forename, Appointments.patientno FROM Doctors INNER JOIN Appointments ON Doctors.Doctorno=Appointments.Doctorno WHERE Doctors.Doctorno='890';

7. What appointment sare there tomorrow?


SELECT * FROM Appointments where Appdate='2/26/2012';

8. What is the total fee for appointments on 6/6/2011?


SELECT SUM(fee) AS [Fees for Given Date] FROM Appointments WHERE Appdate='6/6/2011';

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')

10. What is the total number of appointments in any given day?


SELECT Appdate, COUNT(*) AS [No of Appointments per Day] FROM Appointments GROUP BY Appdate;

11. Who is the oldest Patient in the records?


SELECT MIN(DOB) AS [DOB for Oldest Patient is] FROM Patients SELECT DOB AS [Oldest Patient is:], Patientno, Surname, Forename FROM Patients WHERE DOB IN (SELECT MIN(DOB)FROM Patients);

Page 2 of 5

12. List appointments for all doctors


SELECT Doctors.surname,Appointments.appdate FROM Doctors, Appointments WHERE Doctors.Doctorno=Appointments.Doctorno SELECT distinct Doctors.surname,Appointments.appdate FROM Doctors, Appointments WHERE Doctors.Doctorno=Appointments.Doctorno

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

14. How many doctors have more than 5 patients on 6/16/2011?


SELECT Appointments.Doctorno FROM Appointments GROUP BY Doctorno HAVING COUNT(patientno)>5 SELECT Appointments.Doctorno FROM Appointments WHERE appdate='6/6/2011' GROUP BY Doctorno HAVING COUNT(patientno)>5

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

May also use LEFT OUTER JOIN 17. Union Operator


SELECT Patientno, Surname FROM Patients UNION SELECT Doctorno, Surname FROM Doctors

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