Hospital Database PROF. Dr. Sherif Kassem Adnan Al-kuwaiti 208008705 Redha al-mulla 209011421
Dec 30, 2015
Referential Integrity Constraints
salary DNO ADDRESS SEX SSN L_NAME F_NAME
MGRSSN NUM_DEPARTMENT N_DEPARTMENT
DEP_NO N0_TREAT SUPER_NUMBER PA_NUMBER
SEX KIND _OF _SICK ROOM_NUMBER ENTER_DATE P_NUMBER P_NAME
doctors
Department
PATIENTSANDSUPERVISORS
PHARMACY
PATIENTS
COST No_medication No_pharmacist NO_TREATMENT
PHARM_NAME NO_PHARM ADDRESSPHARMACISTS
SALARY DNO ADDRESS SEX SSN L_NAME F_NAME DOCTORS15000 9 Dammam M 321 Salem Shref
13000 9 Riyadh M 761 Ahmed Ali
12500 1 Dammam F 178 Moaty Noor
11500 5 Hufuf M 367 Ahmed Salman
11000 1 Hufuf F 101 Abdula Smera
13600 9 Riyadh M 203 Hosne Tamer
17000 5 Dammam F 430 Shref Faten
Relational Database
Relational Database
DEP_NO NO_TREAT SUPER_NUMBER PA_NUMBER PATIENTSANDSUPERVISORS
1 1711 178 13
9 7111 761 36
5 3111 367 48
1 1011 101 90
9 2111 203 72
5 4111 430 56
5 1122 430 35
MGRSSN NUM_DEPARTMENT N_DEPARTMENT DEPARTMENT
321 9 Esoteric Clinic
367 5 Teeth Clinic
178 1 Surgery Clinic
COST No_medication No_pharmacist NO_TREATMENT Pharmacy
23 54444 123 1011
22 44225 612 1711
11 78888 612 1711
100 2211 123 3111
Relational Database
ADDRESS NO_PHARM PHARM_NAME PHARMACISTS
ALHSA, ALHOFOF 123 ALI
ALHSA, ALMUBARAZ 612 FAHD
SEX KIND _OF _SICK
ROOM_NUMBER ENTER_DATE P_NUMBER P_NAME PATIENTS
M Heart Surgery 102 2008/5/23 13 Ali
M Worm Traling 303 2008/4/5 36 Saad
F Withdrawal of nerve
201 2009/2/9 48 May
F Neurosurgery 101 2009/1/10 90 Sara
M Customized Ulcer 301 2009/3/30 72 Moath
M Remove Tooth 203 2009/6/15 56 Saleh
M Remove Tooth 222 10/1/2009 35 mohammad
Relational Database
2/ For male patient , list the name of the patient ,the name of doctor is supervisor him, No. department supervised him, and salary.
PATIENTS_ MALE P_NUMBER(σ sex=‘m’ (PATIENTS)) P_ M _PS (PATIENTS_ MALE P_NUMBER=PA_NUMBER PATIENTSANDSUPERVISORS)
Result P_NAME,DEP_NO,F_NAME,SALARY (P_ M _PS * DOCTORS)
SALARY F_NAME DEP_NO P_NAME
17000 Faten 5 MOHAMMAD
12500 Noor 1 Ali
13600 Tamer 9 Moath
17000 Faten 5 Saleh
13000 Ali 9 Saad
2/ For male patient , list the name of the patient ,the name of doctor is supervisor him, No. department supervised him and, salary.
SELECT P_NAME, DEP_NO, F_NAME , SALARYFROM PATIENTS, PATIENTSANDSUPERVISORS,
DOCTORSWHERE PA_NUMBER=P_NUMBER AND
SUPER_NUMBER=SSN AND PATIENTS.SEX='M';
SALARY F_NAME DEP_NO P_NAME
17000 Faten 5 MOHAMMAD
12500 Noor 1 Ali
13600 Tamer 9 Moath
17000 Faten 5 Saleh
13000 Ali 9 Saad
2A/ For male patient , list the name of the patient ,the name of doctor is supervisor him, No. department supervised him and, salary.
SELECT P_NAME, DEP_NO, F_NAME , SALARY FROM ((PATIENTS JOIN PATIENTSANDSUPERVISORS ON
PA_NUMBER=P_NUMBER)JOIN DOCTORS ON SUPER_NUMBER=SSN) WHERE PATIENTS.SEX='M';
SALARY F_NAME DEP_NO P_NAME
17000 Faten 5 MOHAMMAD
12500 Noor 1 Ali
13600 Tamer 9 Moath
17000 Faten 5 Saleh
13000 Ali 9 Saad
4/ Retrieve numbers all patients who are either supervised by the department managed by Female ,or supervised by the department is ‘Teeth Clinic’.
Female_DOC ssn(σ sex=‘f’ (DOCTORS)) F_E_Dep (Female-DOC SSN=MGRSSN Department)F_P PA_NUMBER(F-E-Dep * PATIENTSANDSUPERVISORS)
Tclinc-Dep NUM_DEPARTMENT(σ N_DEPARTMENT='TeethClinic‘ (DEPARTMENT)) T_D_PSuper PA_NUMBER (Tclinc-Dep * PATIENTSANDSUPERVISORS)
Result ( F_P T_D_Psuper )
PA_NUMBER
13
48
56
90
4/Retrieve numbers all patients who are either supervised by the department managed by Female ,or supervised by the department Teeth Clinic.
(SELECT DISTINCT PA_NUMBERFROM PATIENTSANDSUPERVISORS, DEPARTMENT, DOCTORSWHERE DEP_NO = NUM_DEPARTMENT AND MGRSSN=SSN
AND SEX='f')Union ( SELECT DISTINCT PA_NUMBERFROM PATIENTSANDSUPERVISORS, DEPARTMENTWHERE DEP_NO = NUM_DEPARTMENT AND
N_DEPARTMENT='TeethClinic');
PA_NUMBER13485690
4A/Retrieve numbers all patients who are either supervised by the department managed by Female, or supervised by the department Teeth Clinic.
SELECT DISTINCT PA_NUMBERFROM PATIENTSANDSUPERVISORSWHERE PA_NUMBER IN (SELECT PA_NUMBERFROM PATIENTSANDSUPERVISORS, DEPARTMENT, DOCTORSWHERE DEP_NO = NUM_DEPARTMENT AND MGRSSN=SSN AND
DOCTORS.SEX='F')ORPA_NUMBER IN (SELECT PA_NUMBERFROM PATIENTSANDSUPERVISORS, DEPARTMENT WHERE DEP_NO = NUM_DEPARTMENT AND N_DEPARTMENT='TEETHCLINIC');
PA_NUMBER13485690
10/ Find all combinations of PATIENT P_NUMBER and DEPARTMENT N_DEPARTMENT.
SELECT P_NUMBER,N_DEPARTMENT FROM PATIENTS, DEPARTMENT ;
P_NUMBER N_DEPARTMENT
13 SurgeryClinic
36 SurgeryClinic
48 SurgeryClinic
56 SurgeryClinic
72 SurgeryClinic
90 SurgeryClinic
13 TeethClinic
36 TeethClinic
48 TeethClinic
56 TeethClinic
72 TeethClinic
90 TeethClinic
13 EsotericClinic
36 EsotericClinic
48 EsotericClinic
56 EsotericClinic
72 EsotericClinic
90 EsotericClinic
q10A/ RETRIEVE ALL EMPLOYEE WHO WORK IN ALL DEPARTMENT
MGRSSN NUM_DEPARTMENT N_DEPARTMENT SALARY DNO ADDRESS SEX SSN L_NAME F_NAME
178 1 SurgeryClinic 11000 1 Hufuf F 101 Abdula Smera
178 1 SurgeryClinic 12500 1 Dammam F 178 Moaty Noor
178 1 SurgeryClinic 13600 9 Riyadh M 203 Hosne Tamer
178 1 SurgeryClinic 15000 9 Dammam M 321 Salem Shref
178 1 SurgeryClinic 11500 5 Hufuf M 367 Ahmed Salman
178 1 SurgeryClinic 17000 5 Dammam F 430 Shref Faten
178 1 SurgeryClinic 13000 9 Riyadh M 761 Ahmed Ali
367 5 TeethClinic 11000 1 Hufuf F 101 Abdula Smera
367 5 TeethClinic 12500 1 Dammam F 178 Moaty Noor
367 5 TeethClinic 13600 9 Riyadh M 203 Hosne Tamer
367 5 TeethClinic 15000 9 Dammam M 321 Salem Shref
367 5 TeethClinic 11500 5 Hufuf M 367 Ahmed Salman
367 5 TeethClinic 17000 5 Dammam F 430 Shref Faten
367 5 TeethClinic 13000 9 Riyadh M 761 Ahmed Ali
321 9 EsotericClinic 11000 1 Hufuf F 101 Abdula Smera
321 9 EsotericClinic 12500 1 Dammam F 178 Moaty Noor
321 9 EsotericClinic 13600 9 Riyadh M 203 Hosne Tamer
321 9 EsotericClinic 15000 9 Dammam M 321 Salem Shref
321 9 EsotericClinic 11500 5 Hufuf M 367 Ahmed Salman
321 9 EsotericClinic 17000 5 Dammam F 430 Shref Faten
321 9 EsotericClinic 13000 9 Riyadh M 761 Ahmed Ali
SELECT * FROM Doctors,DEPARTMENT ;
Q13 /SHOW THE RESULTING SALARY IF EVERY doctor WORKING IN DEPARTMENT 1 GIVEN 5 PERCENT
SELECT F_NAME,L_NAME ,1.05*SALARY AS INCREASED_SAL
FROM doctors , DEPARTMENT WHERE DNO =NUM_DEPARTMENT AND
N_DEPARTMENT ='SURGERYCLINIC';
INCREASED_SAL L_NAME F_NAME
11550 Abdula Smera
13125 Moaty Noor
14/Retrieve all male patients whose room number is between 300 and 400 .
SELECT *FROM PATIENTS WHERE (ROOM_NUMBER BETWEEN 300 AND400)AND SEX='M';
P_NAME P_NUMBER ENTER_DATE ROOM_NUMBER KIND _OF _SICK SEX
Saad 36 4/5/2008 303 Worm Traling M
Moath 72 30/3/2009 301 Customized Ulcer M
6/ Retrieve the names of doctors who don’t supervise any patient.
SSN_D SSN (DOCTORS)
SUP_NUM (SSN) SUPER_NUMBER (PATIENTSANDSUPERVISORS)
WITHOUT_SUPER _SSN (SSN_D - SUP_NUM)
RESULT F_NAME,L_NAME (WITHOUT_SUPER _SSN *EMPLOYEE)
L_NAME F_NAME
Salem Shref
6/ Retrieve the names of doctors who don’t supervise any patient.
SELECT F_NAME,L_NAMEFROM doctorsWHERENOT EXISTS (SELECT *FROM PATIENTSANDSUPERVISORS WHERE SSN=SUPER_NUMBER)
L_NAME F_NAME
Salem Shref
20/ Find the sum of the salaries of all doctors who supervise a patient at least work in the department number 9, as well as the maximum salary, the minimum salary, and the average salary.
SELECT SUM (SALARY) AS SUM, MAX (SALARY)AS MAX, MIN (SALARY) AS MIN, AVG (SALARY) AS AVG
FROM (DOCTORS JOIN PATIENTSANDSUPERVISORS ON SSN=SUPER_NUMBER)
WHERE DNO=9 ;AVG MIN MAX SUM
13300 13000 13600 26600
22/ Retrieve The Number Of Patients Supervised By Doctors From Esoteric Clinic Department .
SELECT COUNT(*) AS COUNT_DOCTORSFROM
PATIENTSANDSUPERVISORS,DOCTORS,DEPARTMENTWHERE SUPER_NUMBER=SSN AND
DNO=NUM_DEPARTMENT AND N_DEPARTMENT='EsotericClinic';
COUNT_DOCTORS
2
18/ Find all the names of DOCTORS who do not have address
SELECT F_NAME, L_NAMEFROM EMPLOYEEWHERE ADDRESS IS NULL;
16/ Retrieve SSN of Doctors who supervise the patients have the same sex.
SELECT SSN
FROM DOCTORS
WHERE SSN IN (
SELECT SUPER_NUMBERFROM PATIENTSANDSUPERVISORS , PATIENTS AS P,
DOCTORS AS DWHERE PA_NUMBER=P_NUMBER AND SUPER_NUMBER=SSN
AND D.SEX=P.SEX);
SSN
101
203
761
16A/ Retrieve SSN of Doctors who supervise the patients have the same sex.
SELECT SSNFROM PATIENTSANDSUPERVISORS ,
PATIENTS AS P, DOCTORS AS DWHERE PA_NUMBER=P_NUMBER AND
SUPER_NUMBER=SSN AND D.SEX=P.SEX;
SSN
101
203
761
16B/ Retrieve SSN of Doctors who supervise the patients have the same sex.
SELECT SSN
FROM DOCTORS AS D
WHERE EXISTS (
SELECT *FROM PATIENTSANDSUPERVISORS , PATIENTS AS PWHERE PA_NUMBER=P_NUMBER AND
SUPER_NUMBER=SSN AND D.SEX=P.SEX);
SSN
101
203
761
q1 / Retrieve the name and salary of all doctors who work for the teeth clinic.
SELECT F_NAME, L_NAME, SALARYFROM DOCTORS,DEPARTMENT WHERE N_DEPARTMENT='TEETHCLINIC' AND
NUM_DEPARTMENT=DNO;
SALARY L_NAME F_NAME
11500 Ahmed Salman
17000 Shref Faten
7500 Mohamd Kaled
Q1B /rewrite query 1 assuming that doctors relation the F_Name attribute is called Name and the DNO attribute is called NUM_DEPARTMENT and the N_DEPARTMENT attribute in Department relation is called Name .
SELECT NAME, L_NAME, SALARY FROM DOCTORS ,DEPARTMENT WHERE DOCTORS .NAME='TEETHCLINIC' AND
DOCTORS .NUM_DEPARTMENT= DEPARTMENT.NUM_DEPARTMENT;
SALARY L_NAME NAME
11500 Ahmed Salman
17000 Shref Faten
7500 Mohamd Kaled
Q1B /rewrite query 1 assuming that doctors relation the F_Name attribute is called Name and the DNO attribute is called NUM_DEPARTMENT and the N_DEPARTMENT attribute in Department relation is called Name .
SELECT E.NAME, L_NAME, SALARY FROM DOCTORS AS E,DEPARTMENT AS DWHERE D.NAME='TEETHCLINIC' AND
E.NUM_DEPARTMENT=D.NUM_DEPARTMENT;
SALARY L_NAME NAME
11500 Ahmed Salman
17000 Shref Faten
7500 Mohamd Kaled
Q1C/ Retrieve all ATTRIBUTE of the doctor(s) who work in department 5 .
SELECT *FROM DOCTORSWHERE DNO=5;
SALARY DNO ADDRESS SEX SSN L_NAME F_NAME
11500 5 Hufuf M 367 Ahmed Salman
17000 5 Dammam F 430 Shref Faten
Q1D/ Retrieve all attribute of the employee and all attribute of the department who work for the
teeth clinic.SELECT *FROM DOCTORS,DEPARTMENT WHERE N_DEPARTMENT='TEETHCLINIC' AND
NUM_DEPARTMENT=DNO;
MGRSSN NUM_DEPARTMENT N_DEPARTMENT SALARY DNO ADDRESS SEX SSN L_NAME F_NAME
367 5 TeethClinic 11500 5 Hufuf M 367 Ahmed Salman
367 5 TeethClinic 17000 5 Dammam F 430 Shref Faten
q0/ Retrieve the sex and patient number of patient whose name is ‘Ali’ and who is in room 102.
SELECT SEX,P_NUMBERFROM PATIENTSWHERE P_NAME='ALI' AND
ROOM_NUMBER=102;
P_NUMBER SEX
13 M
Q 11 / Retrieve all the address of every doctor
SELECT All addressFROM doctors ; address
Hufuf
Dammam
Riyadh
Dammam
Hufuf
Dammam
Riyadh
Q 11A / Retrieve all distinct the kind of job of every employee
SELECT DISTINCT addressFROM doctors;
address
Dammam
Hufuf
Riyadh
Q12/ Retrieve all pharmacists whose address is in hofof.
SELECT no_pharmFROM PHARMACISTSWHERE ADDRESS LIKE '%hofof%';
no_pharm
123
Q12A/ Find the name of all PATIENTS who Enter in the hospital In 2008 .
SELECT P_NAMEFROM PATIENTSWHERE ENTER_DATE LIKE ‘_ _0 8 _ _ _ _ _ _’ ;
P_NAME
Ali
Saad
Q24/ For each kind of job, retrieve the name kind of job, the number of employees For each kind of job, and their average salary.
SELECT address, COUNT (*), AVG (SALARY)FROM doctorsGROUP BY address;
AVG (SALARY) (COUNT (*) address
14833 3 Dammam
11250 2 Hufuf
13300 2 Riyadh
3/ Retrieve the name of the doctor who supervise all patients who have kind of sick 'REMOVE TOOTH'
SELECT F_NAME ,L_NAMEFROM DOCTORSWHERE( (SELECT PA_NUMBERFROM PATIENTSANDSUPERVISORSWHERE SUPER_NUMBER=SSN)CONTAINS(SELECT P_NUMBERFROM PATIENTS WHERE KIND_OF_SICK='REMOVE TOOTH') );L_NAME F_NAME
Shref Faten
28/ For each Doctor supervise on more than one patient, Find the doctors number who are making more than 4000 and the number of their patients who supervise them .
SELECT SSN, COUNT (*)FROM DOCTORS, PATIENTSANDSUPERVISORSWHERE SSN=SUPER_NUMBER AND SALARY>4000
ANDSUPER_NUMBER IN (SELECT SUPER_NUMBERFROM PATIENTSANDSUPERVISORSGROUP BY SUPER_NUMBERHAVING COUNT (*) > 1)GROUP BY SSN; (COUNT (*) SSN
2 430
8b/ Find all patients and the pharmacist who sold him.
SELECT distinct pa_number,No_pharmacist FROM ( PATIENTSANDSUPERVISORS left JOIN
PHARMACY on no_treat= no_treatment );
No_pharmacist pa_number
612 13
NULL 35
NULL 36
123 48
NULL 56
NULL 72
123 90
Q 15/ Retrieve a list of department and doctor who works in it, ordered alphabetically by last name and first name.
SELECT N_DEPARTMENT,L_NAME,F_NAME FROM DEPARTMENT ,DOCTORS WHERE DNO =NUM_DEPARTMENT ORDER BY L_NAME, F_NAME;
F_NAME L_NAME N_DEPARTMENT
Smera Abdula SurgeryClinic
Ali Ahmed EsotericClinic
Salman Ahmed TeethClinic
Tamer Hosne EsotericClinic
Noor Moaty SurgeryClinic
Shref Salem EsotericClinic
Faten Shref TeethClinic