Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROF. Dr. Sherif Kassem Adnan Al-kuwaiti 208008705 Redha al-mulla 209011421.

Similar presentations


Presentation on theme: "PROF. Dr. Sherif Kassem Adnan Al-kuwaiti 208008705 Redha al-mulla 209011421."— Presentation transcript:

1 PROF. Dr. Sherif Kassem Adnan Al-kuwaiti Redha al-mulla

2 Referential Integrity Constraints Referential Integrity Constraints salary DNOADDRESSSEXSSNL_NAMEF_NAME MGRSSNNUM_DEPARTMENTN_DEPARTMENT DEP_NON0_TREATSUPER_NUMBERPA_NUMBER SEXKIND _OF _SICKROOM_NUMBERENTER_DATEP_NUMBERP_NAME doctors Department PATIENTSANDSUPERVISORS PHARMACY PATIENTS COSTNo_medicationNo_pharmacistNO_TREATMENT PHARM_NAMENO_PHARMADDRESS PHARMACISTS

3 SALARYDNOADDRESSSEXSSNL_NAMEF_NAMEDOCTORS DammamM321SalemShref RiyadhM761AhmedAli DammamF178MoatyNoor HufufM367AhmedSalman HufufF101AbdulaSmera RiyadhM203HosneTamer DammamF430ShrefFaten Relational Database Relational Database

4 DEP_NONO_TREATSUPER_NUMBERPA_NUMBERPATIENTSANDSUPERVISORS MGRSSNNUM_DEPARTMENTN_DEPARTMENTDEPARTMENT 3219Esoteric Clinic 3675Teeth Clinic 1781Surgery Clinic

5 COSTNo_medicationNo_pharmacistNO_TREATMENT Pharmacy Relational Database Relational Database ADDRESSNO_PHARMPHARM_NAME PHARMACISTS ALHSA, ALHOFOF123ALI ALHSA, ALMUBARAZ612FAHD

6 SEX KIND _OF _SICK ROOM_NUMBERENTER_DATEP_NUMBERP_NAMEPATIENTS MHeart Surgery /5/2313Ali MWorm Traling /4/536Saad F Withdrawal of nerve /2/948May FNeurosurgery /1/1090Sara MCustomized Ulcer /3/3072Moath MRemove Tooth /6/1556Saleh MRemove Tooth22210/1/200935mohammad Relational Database

7 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) SALARYF_NAMEDEP_NOP_NAME 17000Faten5MOHAMMAD 12500Noor1Ali 13600Tamer9Moath 17000Faten5Saleh 13000Ali9Saad

8 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, SALARY FROM PATIENTS, PATIENTSANDSUPERVISORS, DOCTORS WHERE PA_NUMBER=P_NUMBER AND SUPER_NUMBER=SSN AND PATIENTS.SEX='M'; SALARYF_NAMEDEP_NOP_NAME 17000Faten5MOHAMMAD 12500Noor1Ali 13600Tamer9Moath 17000Faten5Saleh 13000Ali9Saad

9 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'; SALARYF_NAMEDEP_NOP_NAME 17000Faten5MOHAMMAD 12500Noor1Ali 13600Tamer9Moath 17000Faten5Saleh 13000Ali9Saad

10 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

11 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_NUMBER FROM PATIENTSANDSUPERVISORS, DEPARTMENT, DOCTORS WHERE DEP_NO = NUM_DEPARTMENT AND MGRSSN=SSN AND SEX='f') Union ( SELECT DISTINCT PA_NUMBER FROM PATIENTSANDSUPERVISORS, DEPARTMENT WHERE DEP_NO = NUM_DEPARTMENT AND N_DEPARTMENT='TeethClinic'); PA_NUMBER

12 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_NUMBER FROM PATIENTSANDSUPERVISORS WHERE PA_NUMBER IN (SELECT PA_NUMBER FROM PATIENTSANDSUPERVISORS, DEPARTMENT, DOCTORS WHERE DEP_NO = NUM_DEPARTMENT AND MGRSSN=SSN AND DOCTORS.SEX='F') OR PA_NUMBER IN (SELECT PA_NUMBER FROM PATIENTSANDSUPERVISORS, DEPARTMENT WHERE DEP_NO = NUM_DEPARTMENT AND N_DEPARTMENT='TEETHCLINIC'); PA_NUMBER

13 Q9/ Select all PATIENT NUMBER SELECT P_NUMBER FROM PATIENTS; P_NUMBER

14 10/ Find all combinations of PATIENT P_NUMBER and DEPARTMENT N_DEPARTMENT. SELECT P_NUMBER,N_DEPARTMENT FROM PATIENTS, DEPARTMENT ; P_NUMBERN_DEPARTMENT 13SurgeryClinic 36SurgeryClinic 48SurgeryClinic 56SurgeryClinic 72SurgeryClinic 90SurgeryClinic 13TeethClinic 36TeethClinic 48TeethClinic 56TeethClinic 72TeethClinic 90TeethClinic 13EsotericClinic 36EsotericClinic 48EsotericClinic 56EsotericClinic 72EsotericClinic 90EsotericClinic

15 q10A/ RETRIEVE ALL EMPLOYEE WHO WORK IN ALL DEPARTMENT MGRSSNNUM_DEPARTMENTN_DEPARTMENTSALARYDNOADDRESSSEXSSNL_NAMEF_NAME 1781SurgeryClinic110001HufufF101AbdulaSmera 1781SurgeryClinic125001DammamF178MoatyNoor 1781SurgeryClinic136009RiyadhM203HosneTamer 1781SurgeryClinic150009DammamM321SalemShref 1781SurgeryClinic115005HufufM367AhmedSalman 1781SurgeryClinic170005DammamF430ShrefFaten 1781SurgeryClinic130009RiyadhM761AhmedAli 3675TeethClinic110001HufufF101AbdulaSmera 3675TeethClinic125001DammamF178MoatyNoor 3675TeethClinic136009RiyadhM203HosneTamer 3675TeethClinic150009DammamM321SalemShref 3675TeethClinic115005HufufM367AhmedSalman 3675TeethClinic170005DammamF430ShrefFaten 3675TeethClinic130009RiyadhM761AhmedAli 3219EsotericClinic110001HufufF101AbdulaSmera 3219EsotericClinic125001DammamF178MoatyNoor 3219EsotericClinic136009RiyadhM203HosneTamer 3219EsotericClinic150009DammamM321SalemShref 3219EsotericClinic115005HufufM367AhmedSalman 3219EsotericClinic170005DammamF430ShrefFaten 3219EsotericClinic130009RiyadhM761AhmedAli SELECT * FROM Doctors,DEPARTMENT ;

16 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_SALL_NAMEF_NAME 11550AbdulaSmera 13125MoatyNoor

17 14/Retrieve all male patients whose room number is between 300 and 400. SELECT * FROM PATIENTS WHERE (ROOM_NUMBER BETWEEN 300 AND 400)AND SEX='M'; P_NAMEP_NUMBERENTER_DATEROOM_NUMBERKIND _OF _SICKSEX Saad364/5/ Worm TralingM Moath7230/3/ Customized UlcerM

18 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_NAMEF_NAME SalemShref

19 6/ Retrieve the names of doctors who don’t supervise any patient. SELECT F_NAME,L_NAME FROM doctors WHERE NOT EXISTS (SELECT * FROM PATIENTSANDSUPERVISORS WHERE SSN=SUPER_NUMBER) L_NAMEF_NAME SalemShref

20 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 ; AVGMINMAXSUM

21 22/ Retrieve The Number Of Patients Supervised By Doctors From Esoteric Clinic Department. SELECT COUNT(*) AS COUNT_DOCTORS FROM PATIENTSANDSUPERVISORS,DOCTORS,DEPARTMENT WHERE SUPER_NUMBER=SSN AND DNO=NUM_DEPARTMENT AND N_DEPARTMENT='EsotericClinic'; COUNT_DOCTORS 2

22 18/ Find all the names of DOCTORS who do not have address SELECT F_NAME, L_NAME FROM EMPLOYEE WHERE ADDRESS IS NULL;

23 16/ Retrieve SSN of Doctors who supervise the patients have the same sex. SELECT SSN FROM DOCTORS WHERE SSN IN ( SELECT SUPER_NUMBER FROM PATIENTSANDSUPERVISORS, PATIENTS AS P, DOCTORS AS D WHERE PA_NUMBER=P_NUMBER AND SUPER_NUMBER=SSN AND D.SEX=P.SEX); SSN

24 16A/ Retrieve SSN of Doctors who supervise the patients have the same sex. SELECT SSN FROM PATIENTSANDSUPERVISORS, PATIENTS AS P, DOCTORS AS D WHERE PA_NUMBER=P_NUMBER AND SUPER_NUMBER=SSN AND D.SEX=P.SEX; SSN

25 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 P WHERE PA_NUMBER=P_NUMBER AND SUPER_NUMBER=SSN AND D.SEX=P.SEX); SSN

26 doctors q1 / Retrieve the name and salary of all doctors who work for the teeth clinic. SELECT F_NAME, L_NAME, SALARY FROM DOCTORS,DEPARTMENT WHERE N_DEPARTMENT='TEETHCLINIC' AND NUM_DEPARTMENT=DNO; SALARYL_NAMEF_NAME 11500AhmedSalman 17000ShrefFaten 7500MohamdKaled

27 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; SALARYL_NAMENAME 11500AhmedSalman 17000ShrefFaten 7500MohamdKaled

28 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 D WHERE D.NAME='TEETHCLINIC' AND E.NUM_DEPARTMENT=D.NUM_DEPARTM ENT; SALARYL_NAMENAME 11500AhmedSalman 17000ShrefFaten 7500MohamdKaled

29 Q1C/ Retrieve all ATTRIBUTE of the doctor(s) who work in department 5. SELECT * FROM DOCTORS WHERE DNO=5; SALARYDNOADDRESSSEXSSNL_NAMEF_NAME HufufM367AhmedSalman DammamF430ShrefFaten

30 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; MGRSSNNUM_DEPARTMENTN_DEPARTMENTSALARYDNOADDRESSSEXSSNL_NAMEF_NAME 3675TeethClinic115005HufufM367AhmedSalman 3675TeethClinic170005DammamF430ShrefFaten

31 q0/ Retrieve the sex and patient number of patient whose name is ‘Ali’ and who is in room 102. SELECT SEX,P_NUMBER FROM PATIENTS WHERE P_NAME='ALI' AND ROOM_NUMBER=102; P_NUMBERSEX 13M

32 Q 11 / Retrieve all the address of every doctor SELECT All address FROM doctors ; address Hufuf Dammam Riyadh Dammam Hufuf Dammam Riyadh

33 Q 11A / Retrieve all distinct the kind of job of every employee SELECT DISTINCT address FROM doctors; address Dammam Hufuf Riyadh

34 Q12/ Retrieve all pharmacists whose address is in hofof. SELECT no_pharm FROM PHARMACISTS WHERE ADDRESS LIKE '%hofof%'; no_phar m 123

35 Q12A/ Find the name of all PATIENTS who Enter in the hospital In SELECT P_NAME FROM PATIENTS WHERE ENTER_DATE LIKE ‘_ _0 8 _ _ _ _ _ _’ ; P_NAME Ali Saad

36 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 doctors GROUP BY address; AVG (SALARY)(COUNT (*)address Dammam Hufuf Riyadh

37 3/ Retrieve the name of the doctor who supervise all patients who have kind of sick 'REMOVE TOOTH' SELECT F_NAME,L_NAME FROM DOCTORS WHERE( (SELECT PA_NUMBER FROM PATIENTSANDSUPERVISORS WHERE SUPER_NUMBER=SSN) CONTAINS (SELECT P_NUMBER FROM PATIENTS WHERE KIND_OF_SICK='REMOVE TOOTH') ); L_NAMEF_NAME ShrefFaten

38 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, PATIENTSANDSUPERVISORS WHERE SSN=SUPER_NUMBER AND SALARY>4000 AND SUPER_NUMBER IN (SELECT SUPER_NUMBER FROM PATIENTSANDSUPERVISORS GROUP BY SUPER_NUMBER HAVING COUNT (*) > 1) GROUP BY SSN; (COUNT (*)SSN 2430

39 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_pharmacistpa_number NULL35 NULL NULL56 NULL

40 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_NAMEL_NAMEN_DEPARTMENT SmeraAbdulaSurgeryClinic AliAhmedEsotericClinic SalmanAhmedTeethClinic TamerHosneEsotericClinic NoorMoatySurgeryClinic ShrefSalemEsotericClinic FatenShrefTeethClinic

41 Q7 /LIST THE NAMES OF MANAGERS WHO HAVE AT LEAST ONE PATIENTS SELECT F_NAME,L_NAME FROM DOCTORS WHERE EXISTS (SELECT * FROM PATIENTSANDSUPERVISORS WHERE SUPER_NUMBER=SSN ) AND EXISTS (SELECT * FROM DEPARTMENT WHERE MGRSSN=SSN); L_NAMEF_NAME MoatyNoor AhmedSalman


Download ppt "PROF. Dr. Sherif Kassem Adnan Al-kuwaiti 208008705 Redha al-mulla 209011421."

Similar presentations


Ads by Google