====== SQL Exercises ======
We have a hospital database:
**PAT** (__PID__, PNA, PAD, PDB, PTE)
**DOC** (__DID__, DNA, SPE)
**VIS** (__PID, DID, DVI__, DIA, TYP, PRI)
Here is the [[https://users.fit.cvut.cz/~valenta/bie-dbs/seminars/doctor_cre.sql|script which creates doctor database and initialize it by some data]].
And here are the queries:
1. List of all doctor's specializations
select distinct spe from doc;
2. Names of all orthopeds.
select dna
from doc
where upper(spe) = 'ORTHOPED';
3. Names and addresses of all patients borned before 1920.
select pna
from pat
where to_char(pdb, 'YYYY') < 1920;
4. DIDs of doctors visited by patient Scott.
select distinct did
from vis join pat using (pid)
where upper(pna) = 'SCOTT';
5. Names of internist visited by patient Scott.
select distinct dna
from (doc join vis using (did)) join pat using (pid)
where upper (spe) = 'INTERNIST' and upper (pna) = 'SCOTT';
6. Names and addresses of patients visited by doctor Jones.
select pna, pad
from (pat join vis using (pid)) join doc using (did)
where upper(dna) = 'JONES';
7. Names and address of patients visited only by doctor Jones (i.e. by no other doctor).
select pna, pad
from (pat join vis using (pid)) join doc using (did)
where upper(dna) = 'JONES'
minus
select pna, pad
from (pat join vis using (pid)) join doc using (did)
where upper(dna) <> 'JONES'
8. Two-tuples of names of patients living in the same address.
select p1.pna, p2.pna
from pat p1 join pat p2 on (p1.pad = p2.pad and p1.pid < p2.pid);
9. The doctors who already had patients.
select distinct d.*
from doc d join vis v on (d.did = v.did);
10. Names of doctors who have no patients yet.
select * from doc
minus
select distinct d.*
from doc d join vis v on (d.did = v.did);
11. Which specialization did not work yet.
select * from doc
minus
select distinct d.*
from doc d join vis v on (d.did = v.did);
12. Names and addresses of patients with diagnose D1 (do not use a natural join for this query).
select pna, pad
from pat join vis on (pat.pid = vis.pid and dia = 'D1');
13. The patients who visited any doctor.
select distinct pat.*
from pat join vis on (pat.pid = vis.pid)
14. The patients who did not visited each doctor.
select *
from pat
where pid in
(select distinct pid from
(select p.pid, v.did
from ((select pid from pat) p cross join
(select distinct did from vis) v )
minus
(select pid, did from vis)));
15. The patients who visited all doctors who had any patient.
select * from pat
minus
select *
from pat
where pid in
(select distinct pid from
(select p.pid, v.did
from ((select pid from pat) p cross join
(select distinct did from vis) v )
minus
(select pid, did from vis)));
16. The patients who visited at least one othoped.
select distinct pid, pna, pad, pdb, pte
from pat join vis using (pid) join doc using (did)
where upper(spe) = 'ORTHOPED';
17. Compute the sum of money spent by all the patients together for all visits.
18. What is maximal price of the visit.
19. Patients who paid maximal price for their visit.
20. For each patient display the average of money spent for their visits. Order the output by the amount of visits.
21. As 20, but include also patients which were not on visit.
22. as 20, but include just patients who were at lest on three visits.