250 likes | 398 Views
SQL Exercises – Part II. October 31, 2014. Exercise 1. Consider the following relational database scheme: Treatment (disease, medication) Doctor (name, disease-of-specialization) Treated (doctor_name, patient_name, date, procedure, diagnostic)
E N D
SQL Exercises – Part II October 31, 2014
Exercise 1 • Consider the following relational database scheme: • Treatment (disease, medication) • Doctor(name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • Patients and doctors are uniquely identified by their name • A patient may suffer from several diseases, and may take several medications for each disease. • It is possible to a doctor to be a patient and vice-versa. • The domains of the attributes "disease" and "disease-of-specialization" are the same, namely, the set of all diseases. • In "treated" relation, the procedure could be consultation, or intervention (surgery etc.) and the diagnostic could be the name of a disease or the type of intervention. • Write the following queries in SQL:
Exercise 1 (a) • Treatment (disease, medication) • Doctor (name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • Give the name of doctors who don't suffer from any disease.
Solution 1 (a) – Method 1 • Give the name of doctors who don't suffer from any disease. DoctorsWhoSuffered(name(name=patient_name (Doctor Treated))) name(Doctor) – DoctorsWhoSuffered • In SQL: SELECT distinct name FROM Doctor EXCEPT SELECT name FROM Doctor, Treated WHERE name = patient_name
Solution 1 (a) – Method 2 • Give the name of doctors who don't suffer from any disease. SELECT distinctd.name FROM DOCTORd WHERE NOT EXISTS (SELECT * FROM TREATEDt WHERE d.name = t.patient_name);
Exercise 1 (b) • Treatment (disease, medication) • Doctor (name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • List patients who suffer from more than one disease.
Solution 1 (b) • List patients who suffer from more than one disease. Patient1(name1, diagnostic1) (patient_name, diagnostic( Treated)) Patient2(name2, diagnostic2) (Patient1) name1(name1=name2 AND diagnostic1 <> diagnostic2 (Patient1 Patient2)) • In SQL: SELECT distinct p1.patient_name FROM TREATEDp1, TREATEDp2 WHERE p1.patient_name = p2.patient_name AND p1.diagnostic <> p2.diagnostic;
Exercise 1 (c) • Treatment (disease, medication) • Doctor (name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • Give the names of doctors who are also patients suffering from a disease in their own specialization.
Solution 1 (c) • Give the names of doctors who are also patients suffering from a disease in their own specialization. name(name=patient_name AND disease-of-specialty = diagnostic(Doctor Treated)) • In SQL: SELECT distinct name FROM DOCTOR, TREADTED WHERE name = patient_name AND disease-of-specialization = diagnostic;
Exercise 1 (d) • Treatment (disease, medication) • Doctor (name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • Find diseases for which there is only one medication.
Solution 1 (d) – Method 1 • Find diseases for which there is only one medication. DiseaseWithMoreThanOneTreatment (t1.disease (t1.disease=t2.disease AND t1.medication <> t2.medication(t1(Treatment) t2(Treatment)) disease (Treatment) – DiseaseWithMoreThanOneTreatment • In SQL: SELECT distinctdisease FROM Treatment EXCEPT SELECT t1.disease FROM Treatment t1, Treatment t2 WHERE t1.disease =t2.disease AND t1.medication <> t2.medication
Solution 1 (d) – Method 2 • Find diseases for which there is only one medication. SELECT distinctt1.disease FROM TREATMENTt1 WHERE NOT EXISTS (SELECT t2.disease FROM TREATMENT t2 WHERE t1.disease = t2.disease AND t1.medication <> t2.medication);
Exercise 1 (e) • Treatment (disease, medication) • Doctor (name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • Find the names of patient who had an operation done by a doctor with HIV
Solution 1 (e) • Find the names of patient who had an operation done by a doctor with HIV. SELECT distinct t1.patient_name FROM TREATEDt1 WHERE (t1.procedure=“intervention” AND t1.doctor_name IN ( SELECT distinctd1.name FROM TREATEDt2, DOCTORd1 WHERE (t2.patient_name=d1.name AND t2.diagnostic=“HIV”)));
Exercise 1 (f) • Treatment (disease, medication) • Doctor (name, disease-of-specialization) • Treated (doctor_name, patient_name, date, procedure, diagnostic) • List the patients who consulted 2 or more doctors and was given exactly the same diagnosis. List patient’s name, doctor’s name, the date, and the diagnosis.
Solution 1 (f) • List the patients who consulted 2 or more doctors and was given exactly the same diagnosis. List patient’s name, doctor’s name, the date, and the diagnosis. SELECT distinctt1.patient_name, t1.doctor_name, t1.date, t1.diagnostic FROM TREATEDt1, TREATEDt2 WHERE t1.patient_name = t2.patient_name AND t1.doctor_name <> t2.doctor_name AND t1.diagnostic=t2.diagnostic
Exercise 2 • Consider the following relation scheme about hockey teams: • Team (tname, year, coach, salary) • Player (pname, position) • Winner (tname, year) • Played (pname, tname, year, salary) • Assume that players and coaches appointments with teams are on a calendar year basis. • The team relation indicates teams, their coaches on yearly basis and their salaries. • The player relation indicates players and the position each player plays. • The winner relation indicates the Stanley Cup winners. • The played relation indicates players and the team they have played for on a yearly basis. • Write the following queries in SQL:
Exercise 2 (a) • Team (tname, year, coach, salary) • Player (pname, position) • Winner (tname, year) • Played (pname, tname, year, salary) • List all the players who have salary higher than 2M$, and have played for all the teams in the league
Solution 2 (a) • List all the players who have salary higher than 2M$, and have played for all the teams in the league SELECT p1.pname FROM PLAYEDp1 WHERE p1.salary > 20000000 Group byp1.pname Havingcount (distinctp1.tname) = (Select count (distinct team.tname) From TEAM);
Exercise 2 (b) • Team (tname, year, coach, salary) • Player (pname, position) • Winner (tname, year) • Played (pname, tname, year, salary) • List coaches who have also been players, and have coached only teams they once played for.
Solution 2 (b) • List coaches who have also been players, and have coached only teams they once played for. SELECT distinctt1.coach FROM TEAMt1 EXCEPT SELECT distinctt1.coach FROM TEAMt1 WHERE t1.coach NOT IN (SELECT p1.pname FROM PLAYEDp1 WHERE p1.tname=t1.tname);
Exercise 2 (c) • Team (tname, year, coach, salary) • Player (pname, position) • Winner (tname, year) • Played (pname, tname, year, salary) • List all the players who won the Stanley Cup during two consecutive years with two different teams.
Solution 2 (c) • List all the players who won the Stanley Cup during two consecutive years with two different teams. SELECT distincta1.pname FROM(SELECT p.pname, p.tname, p.year FROM PLAYEDp, WINNERw WHERE p.tname = w.tname AND p.year=w.year;) a1, (SELECT p.pname, p.tname, p.year FROM PLAYEDp, WINNERw WHERE p.tname = w.tname AND p.year=w.year;) a2 WHERE a1.pname = a2.pname AND a1.tname <> a2.tname AND (a1.year = a2.year – 1 OR a1.year = a2.year + 1);
Exercise 2 (d) • Team (tname, year, coach, salary) • Player (pname, position) • Winner (tname, year) • Played (pname, tname, year, salary) • List all coaches who earn more than the highest player's salary in the team.
Solution 2 (d) • List all coaches who earn more than the highest player's salary in the team. Select coach From Team Where salary > All (Select salary From Played Where Team.tname=Played.tname)