Вы находитесь на странице: 1из 5

Practical Exam: Sample Database

Given the following tables, please generate the SQL syntax to provide the requested functions and/or reports

Doctor
ID number(4) primary key
name varchar2(20) /* Last,First */

Disease
code char(3) primary key
name varchar2(20)
contagion number(1) /* 0=none, 5=highest */

Expertise
exp_key number(5) primary key /* sequence # */
doctor number(4) references doctor(ID)
disease char(3) references disease(code),

Patient
ID number(5) primary key
name varchar2(20) /* Last,First */
doctor number(4) references doctor(ID)
room char(3) /* 1st character is floor */
disease char(3) references disease(code)
med_ins varchar2(10) /* Name of carrier */
bill_amt number(9,2)
admit_dt date
release_dt date /* null until patient is released */

1, Which patients are contagious (i.e. contagion > 2)?

select p.*
from patient p, disease d
where p.dis_code = d.dis_code and d.contagion > 2

ID NAME DOCTOR ROOM_CODE DIS_CODE MED_INS BILL_AMT ADMIT_DT RELEAS

1001 Armstrong,John 104 310 HEP BCBS 2500 02/23/2018 02/26/20

1005 Earheart,Peter 104 142 TUB Harvard 2900 02/21/2018 -

1006 Falcon,Mary 105 206 HIV BCBS 7450 02/23/2018 02/25/20

1009 Infante,Mario 108 205 FLU BCBS 2500 02/23/2018 02/26/20

1011 Kilroy,Heather 108 103 FLU BCBS 3500 02/27/2018 -

1013 Masters,Rose 104 301 HEP Harvard 9520 02/21/2018 02/25/20

1014 Nestor,Steve 103 309 FLU Harvard 8500 02/23/2018 -


1016 Peterson,John 101 113 TUB United 5290 02/25/2018 -

1018 Rawlins,Eileen - 204 HEP BCBS 1250 02/17/2018 02/22/20

1020 Talbot,Fred 104 305 HEP United 7570 02/23/2018 -

2. For each insurer, which patient has the lowest bill?

select p.med_ins, p.name, p.bill_amt


from patient p
where p.bill_amt = (select max(pa.bill_amt) from patient pa where pa.med_ins = p.med_ins)

MED_INS NAME BILL_AMT

BCBS Falcon,Mary 7450

Harvard Masters,Rose 9520

Medicare OReilly,Sara 7560

United Talbot,Fred 7570

3. How many patients were in the hospital 2 weeks ago today? Subtotal by floor. (Note: “Floor” is the 1st
character of the room number).

select substr(room_code,1,1) "Floor", name, admit_dt, release_dt


from patient
where admit_dt < sysdate-14 and release_dt > sysdate-14
order by 1

no data found

4. Select all current patients that have contagion “5” diseases and all doctors that treat contagion “5” diseases.
Ensure you note whether it is a doctor or patient and sort them by name.
select 'doctor' "TYPE", d.name
from doctor d, expertise e, disease dis
where d.ID = e.ID and e.disease = dis.dis_code AND dis.contagion = 5
UNION
select 'patient', p.name
from patient p join disease dd on p.dis_code = dd.dis_code
where dd.contagion = 5

TYPE NAME

doctor Hunnicut,Mike

doctor Welby.Marcus

doctor Zorba,John

patient Armstrong,John

patient Earheart,Peter

patient Masters,Rose

patient Peterson,John

patient Rawlins,Eileen

patient Talbot,Fred

5. Which disease names end in “itis” and note how contagious they are?

select d.name
from disease d
where d.name like '%itis'

NAME

Hepatitis

6. For each insurance provider, what is the average duration of stay? Round the result to full days.

select med_ins, to_char(avg(case when release_dt is null


then sysdate- admit_dt
else release_dt - admit_dt end), '999.9') "Average Stay"
from patient
group by med_ins
MED_INS Average Stay

Harvard 23.9

United 28.7

BCBS 11.2

Medicare 29.2

7. Select all patient last names and first initials, with a comma (e.g. “Smith,J”), and note what day of the week
they were admitted.
select substr(name,1,instr(name,',')+1) "Name", to_char(admit_dt, 'Day') "Admitted Day of
Week"
from patient

Name Admitted Day of Week

Armstrong,J Friday

Baker,A Monday

Chalice,M Saturday

Driver,M Saturday

Earheart,P Wednesday

Falcon,M Friday

Garrison,J Tuesday

Hampton,K Friday

Infante,M Friday

Johnson,G Tuesday

8. For which diseases are there NO patients in our database?


select d.dis_code
from disease d

MINUS

select p.dis_code
from patient p

no data found

Вам также может понравиться