Hospital Database Management System SQL ORCLPDB.
sql 1/12
--1 details of nurse who is yet to be registered
select *from nurse
where registered ='f';
--2 Write a query in SQL to find the name of the nurse who are the head of t
select *from nurse
where position='Head Nurse';
--3 Write a query in SQL to obtain the name of the physicians who are the he
select p.name,d.name from physician p
inner join department d
on p.employeeid = d.head;
--4 Write a query in SQL to count the number of patients who taken appointme
select count(distinct(patient)) from appointment;
--5 Write a query in SQL to find the floor and block where the room number 2
select blockfloor,blockcode,roomnumber
from room
where roomnumber=212;
--6 Write a query in SQL to count the number available rooms
select count(unavailable)
from room
where unavailable='f';
--7 Write a query in SQL to count the number of unavailable rooms
1.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 2/12
(select count(unavailable)
from room
where unavailable='t')
select *from avlbl;
--8 Write a query in SQL to obtain the name of the physician and the departm
select employeeid,department,p.name as physician_name,d.name as department_na
from physician p
inner join affiliated_with aw
on p.employeeid = aw.physician
inner join department d
on aw.department = d.department_id
where primaryaffiliation='t';
--9 Write a query in SQL to obtain the name of the physicians who are traine
select employeeid,name
from physician
where employeeid in (select distinct physician
from trained_in);
--method 2 using join
select p.employeeid,p.name,pr.code,pr.name as name_of_procedure
from physician p
inner join trained_in ti
on p.employeeid=ti.physician
inner join procedure pr
on ti.treatment=pr.code;
--10 Write a query in SQL to obtain the name of the physicians with departme
select p.name,d.name from physician p
2.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 3/12
on p.employeeid = aw.physician
inner join department d
on aw.department = d.department_id
where primaryaffiliation='f';
--11 Write a query in SQL to obtain the name of the physicians who are not a
select name as not_specialized_physicians
from physician
where employeeid not in(select distinct physician
from trained_in);
--12 Write a query in SQL to obtain the name of the patients with their phys
select p.name as patient_name,ph.name as phy_who_did_pri_treatment
from patient p
inner join physician ph
on p.pcp = ph.employeeid;
--13 Write a query in SQL to find the name of the patients and the number of
select p.name as patient_name,count(distinct physician) as no_of_phy_tkn_apmn
from patient p
inner join appointment a
on p.ssn = a.patient
group by p.name;
--14 Write a query in SQL to count number of unique patients who got an appo
3.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 4/12
from appointment
group by examinationroom
having examinationroom='C';
--15 Write a query in SQL to find the name of the patients and the number of
select p.name as patient_name,s.room as roomnumber
from patient p
inner join stay s
on p.ssn = s.patient
inner join room r
on s.room = r.roomnumber;
--16 Write a query in SQL to find the name of the nurses and the room schedu
select n.employeeid as nurse_id,n.name as nurse_name,room as room_no
from nurse n
inner join undergoes u
on n.employeeid = u.assistingnurse
inner join stay s
on u.stay = s.stayid;
--17 Write a query in SQL to find the name of the patients who taken the app
select p.name as patient_name,
ph.name as physician_name,
n.name as nurse_name,
a.examinationroom
from patient p
left outer join appointment a
on p.ssn = a.patient
left outer join physician ph
on a.physician = ph.employeeid
4.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 5/12
on a.prepnurse = n.employeeid
where start_dt = '25-04-08';
--18 Write a query in SQL to find the name of patients and their physicians
select p.name as patient_name,ph.name as physician_name
from patient p
inner join undergoes u
on p.ssn = u.patient
inner join physician ph
on u.physician = ph.employeeid
where assistingnurse is null;
--19 Write a query in SQL to find the name of the patients, their treating p
select p.ssn,p.name as patient_name,ph.name as treating_phy_name,m.
from patient p
inner join undergoes u
on p.ssn = u.patient
inner join prescribes pr
on u.patient = pr.patient
innerjoin medication m
on pr.medication = m.code
inner join physician ph
on pr.physician = ph.employeeid;
--20 Write a query in SQL to find the name of the patients who taken an adva
select p.ssn,
p.name as patient_name,
5.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 6/12
m.name as medicine_name
from patient p
left outer join appointment a
on p.ssn = a.patient
left outer join prescribes pr
on a.patient = pr.patient
left outer join physician ph
on pr.physician = ph.employeeid
left outer join medication m
on pr.medication = m.code
;
--22 Write a query in SQL to count the number of available rooms in each blo
select blockcode as block_no,count(*) as no_of_available_rooms
from room
where unavailable='f'
group by blockcode;
--23 Write a query in SQL to count the number of available rooms in each flo
select blockfloor as floor_no,count(*) as no_of_available_rooms
from room
where unavailable='f'
group by blockfloor;
--24 Write a query in SQL to count the number of available rooms for each bl
6.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 7/12
from room
where unavailable='f'
group by blockcode,blockfloor
order by 1,2;
--25 Write a query in SQL to count the number of unavailable rooms for each
select blockcode,blockfloor,count(*) no_of_available_rooms_ineachblockineachf
from room
where unavailable='t'
group by blockcode,blockfloor
order by 1,2;
--26 Write a query in SQL to find out the floor where the maximum no of room
select blockfloor,count(unavailable) as available_room
from room
where unavailable='f'
group by blockfloor
order by count(unavailable) desc
fetch first row only;
--27 Write a query in SQL to find out the floor where the minimum no of room
select blockfloor,count(unavailable)
from room
where unavailable = 'f'
group by blockfloor
having count(unavailable)=(select min(count(unavailable))
from room
where unavailable ='f'
group by blockfloor)
order by blockfloor;
7.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 8/12
--28 Write a query in SQL to obtain the name of the patients, their block, f
select ssn as patient_id,
p.name as patient_name,
blockfloor,
blockfloor,
roomnumber
from patient p
inner join stay s
on p.ssn = s.patient
inner join room r
on s.room = r.roomnumber;
--29 Write a query in SQL to obtain the nurses and the block where they are
select employeeid as nurse_id,
name as nurse_name,
blockcode
from nurse n
left outer join on_call oc
on n.employeeid = oc.nurse;
--31 Write a SQL query to obtain the names of all the physicians performed a
select ph.name as physician_name,u.procedure
from physician ph
inner join undergoes u
on ph.employeeid = u.physician
left outer join trained_in ti
on u.physician = ti.physician
and u.procedure = ti.treatment
where treatment is null;
8.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 9/12
--32 Write a query in SQL to obtain the names of all the physicians, their
select p.name as patient_name,ph.name as physician_name,date_ as date_of_proc
pr.name as procedure_name,code as procedure_code
from physician ph
inner join undergoes u
on ph.employeeid = u.physician
left outer join trained_in ti
on u.physician = ti.physician
and u.procedure = ti.treatment
left outer join patient p
on u.patient = p.ssn
left outer join procedure pr
on u.procedure = pr.code
where treatment is null;
--33 Write a query in SQL to obtain the name and position of all physicians
select employeeid,name as physician_name,position
from physician
where employeeid in(select u.physician
from undergoes u
inner join trained_in ti
on u.physician = ti.physician
where date_ > certificationexpires);
--34 Write a query in SQL to obtain the name of all those physicians who com
--date of procedure, name of the patient on which the procedure had been app
select employeeid,
9.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 10/12
ph.position,
pr.name as procedure_name,
u.date_ as date_of_procedure,
p.name as patient_name,
ti.certificationexpires
from physician ph
left outer join undergoes u
on ph.employeeid = u.physician
left outer join patient p
on u.patient = p.ssn
left outer join trained_in ti
on u.procedure = ti.treatment
left outer join procedure pr
on ti.treatment = pr.code
where date_ > certificationexpires;
--35 Write a query in SQL to obtain the names of all the nurses who have eve
select employeeid,name as nurse_name
from nurse
where employeeid in(select oc.nurse from on_call oc
left outer join room r
on oc.blockkfloor = r.blockfloor
and oc.blockcode = r.blockcode
where roomnumber = 122);
--36 Write a query in SQL to Obtain the names of all patients who has been p
--out primary care and the name of that physician
select p.name as patient_name,
ph.name as physician_name
from patient p
left outer join prescribes pr
10.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 11/12
left outer join physician ph
on pr.physician = ph.employeeid
where pcp = pr.physician;
--37 Write a query in SQL to obtain the names of all patients who has been u
--physician who has carried out primary care
select p.ssn,ph.employeeid as physician_id,
p.name as patient_name,
ph.name as primary_care_physician
from patient p
inner join undergoes u
on p.ssn = u.patient
inner join procedure pr
on u.procedure = pr.code
inner join physician ph
on p.pcp = ph.employeeid
where cost > 5000;
--38 Write a query in SQL to Obtain the names of all patients who had at lea
--out primary care
select a.patient as patient_id,p.name as patient_name,ph.name as physician_na
from patient p
inner join appointment a
on p.ssn = a.patient
inner join nurse n
on a.prepnurse = n.employeeid
11.1 / 12 2023.06.24 13:34:22
Hospital Database Management System SQL ORCLPDB.sql 12/12
on p.pcp = ph.employeeid
where registered='t'
group by a.patient,p.name,ph.name
having count(start_dt) >=2;
--39 Write a query in SQL to Obtain the names of all patients whose primary
--department and name of that physician along with their primary care physic
select p.name as patient_name,
ph.name as primary_care_physician_name
from patient p
inner join physician ph
on p.pcp = ph.employeeid
where p.pcp not in(select head from department);
12.1 / 12 2023.06.24 13:34:22