Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,

Similar presentations


Presentation on theme: "1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,"— Presentation transcript:

1 1 Advanced SQL

2 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query the subjects learnt by the 3 rd grade and the teachers who teach them? select subject_name, teacher from subject where class = ‘ 3rd grade ’ Would adding distinct change the result? Only if the same tuples may appear more than once in table subject

3 3 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query the pupils who are taught by the teacher Nurit and in which class? select distinct pupil_name, pupil.class from pupil, subject where pupil.class = subject.class and teacher = ‘ Nurit ’

4 4 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query all pairs of pupils who study in the same class ? select p1.pupil_name, p2.pupil_name from pupil p1, pupil p2 where p1.class = p2.class and p1.pupil_name < p2.pupil_name

5 5 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query the average age of all pupils? select avg(2007  birthyear) from pupil

6 6 Nested Queries: reminder and more

7 7 Example Tables Used Reserves sidbidday 22 58 101 103 10/10/04 11/12/04 Sailors sidsnameratingage 22 31 58 Dustin Lubber Rusty 7 8 10 45.0 55.5 35.0 Boats bidbnamecolor 101 103 Nancy Gloria red green

8 8 Nested queries in WHERE Equality nested query: Select R.bid From Sailors S, Reserves R Where sid = (select sid from S where sname=‘George’); When would this work? When the subquery returns exactly one tuple

9 9 Nested queries in WHERE SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103); Subqueries with multiple results: Names of sailors who reserved boat 103 What would happen if we wrote NOT IN? We would get names of sailors who did not reserve boat 103

10 10 What does this produce? SELECT S.sname FROM Sailors S WHERE S.sid NOT IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (SELECT B.bid FROM Boats B WHERE B.color='red')); Names of sailors who did not reserve a red boat

11 11 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query pupils in the 5 th grade who have a grade higher than 95? (with a subquery) select pupil_name from pupil where class= ‘ 5 th ’ and pupil_name in (select pupil_name from grades where grade>95) can also be written without a subquery …

12 12 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query pupils in the 5 th grade who have a grade higher than 95? (without a subquery) select pupil_name from pupil p, grades g where p.class= ‘ 5 th ’ and p.pupil_name=g.pupil_name and g.grade>95

13 13 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query pupils in the 5 th grade who DO NOT have a grade higher than 95? select pupil_name from pupil where class= ‘ 5 th ’ and pupil_name not in (select pupil_name from grades where grade>95) (cannot be expressed by a simple query as before)

14 14 Set-Comparison Queries SELECT * FROM Sailors S1 WHERE S1.age > ANY (SELECT S2.age FROM Sailors S2); Sailors who are not the youngest We can also use op ALL (op is >, =, ).

15 15 Exists and Not exists SELECT S.sid FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 and S.sid = R.sid); Sid of sailors who reserved boat 103 Q: What if we wrote NOT EXISTS? A: We would get sid of sailors who did not reserve boat 103 S not in subquery, refers to outer loop

16 16 Exists and Not Exists Exists: For every tuple in the outer loop, the inner loop is tested. If the inner loop produces a result, the outer tuple is added to the result. Differs from In: Does not match values, but tests for existence of result

17 17 Rewriting Minus Queries SELECT S.sname, S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS SELECT S.sname, S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’; Name and id of sailors that have reserved a red boat and have not reserved a green boat

18 18 Rewriting Minus Queries Using Not In SELECT S.sname, S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ and S.sid NOT IN ( SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid = B2.bid and B2.color=‘green’);

19 19 Rewriting Minus Queries Using Not Exists SELECT S.sname, S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ and NOT EXISTS ( SELECT * FROM Boats B2, Reserves R2 WHERE R2.sid = S.sid and R2.bid = B2.bid and B2.color = ‘green’);

20 20 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) What does this return? select pupil_name from pupil p where not exists (select task_no from grades where pupil_name= ‘ michal ’ and subject_name= ‘ history ’ and task_no not in (select task_no from grades g where g.pupil_name = p.pupil_name and g.subject_name = ‘ history ’ )) Pupils who submitted all History tasks that michal submitted

21 21 How would you query the sailors who have reserved all boats?

22 22 Division Consider: A(X,Y) and B(Y). Then A  B = In general, we require that the set of fields in B be contained in those of A.

23 23 Suppliers from A who supply All Parts from B (1) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A P2 pno B1 sno  =

24 24 Suppliers from A who supply All Parts from B (1) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A P2 pno B1 sno  = S1 S2 S3 S4

25 25 Suppliers from A who supply All Parts from B (2) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A sno  = P2 P4 pno B2

26 26 Suppliers from A who supply All Parts from B (2) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A sno  = P2 P4 pno B2 S1 S4

27 27 Suppliers from A who supply All Parts from B (3) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A sno  = P1 P2 P4 pno B3

28 28 Suppliers from A who supply All Parts from B (3) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A sno  = P1 P2 P4 pno B3 S1

29 29 Sailors who Reserved all Boats Reserves(sid,bid)  Boats(bid) Sailor S whose "set of boats reserved" contains the "set of all boats" So what is the result of this?

30 30 Sailors who reserved all boats (Division 1) SELECT sid FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE B.bid NOT IN (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid)); Sailors for which there does not exist a boat that they did not reserve

31 31 Sailors who reserved all boats (Division 2) SELECT S.sid FROM Sailors S WHERE NOT EXISTS( SELECT B.bid FROM Boats B WHERE NOT EXISTS( SELECT R.bid FROM Reserves R WHERE R.bid=B.bid and R.sid=S.sid)) Sailors for which there does not exist a boat that they did not reserve

32 32 Sailors who reserved all boats (Division 3) SELECT S.sid FROM Sailors S WHERE NOT EXISTS((SELECT B.bid FROM Boats B) MINUS (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid)); Sailors for which there does not exist a boat for which there is no reservation in Reserves

33 33 Aggregation

34 34 Aggregate Operators The aggregate operators available in SQL are: –COUNT(*) –COUNT([DISTINCT] A) –SUM([DISTINCT] A) –AVG([DISTINCT] A) –MAX(A) –MIN(A) NULL values are ignored

35 35 Some Examples SELECT COUNT(*) FROM Sailors S SELECT AVG(S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT(distinct color) FROM Boats SELECT COUNT(sid) FROM Sailors S 

36 36 Find Average Age for each Rating So far, aggregation has been applied to all tuples that passed the WHERE clause test. How can we apply aggregation to groups of tuples?

37 37 Find Average Age for each Rating SELECT AVG(age) FROM Sailors GROUP BY rating;

38 38 Basic SQL Query SELECT [Distinct] attributes FROM relation-list WHERE condition GROUP BY grouping-attributes HAVING group-condition; attributes: must appear in grouping-attributes or aggregation operators group-condition: Constrains groups. Can only constrain attributes appearing in grouping-attributes or aggregation operators

39 39 Evaluation- important! 1.Compute cross product of relation-list 2.Tuples failing condition are thrown away 3.Tuples are partitioned into groups by values of grouping-attributes 4.The group-condition is applied to eliminate groups 5.One answer in generated for each group! SELECT [Distinct] attributes FROM relation-list WHERE condition GROUP BY grouping-attributes HAVING group-condition;

40 40 Sailors sidSnameratingage 22 31 58 63 78 84 Dustin Lubber Rusty Fluffy Morley Popeye 7 8 10 7 10 45.0 55.5 35.0 44.0 31.0 33.0 Sailors sidsnameratingage 22 63 78 31 58 84 Dustin Fluffy Morley Lubber Rusty Popeye 7 8 10 45.0 44.0 31.0 55.5 35.0 33.0 40 55.534 SELECT AVG(age) FROM Sailors GROUP BY rating;

41 41 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; SidSnameratingage 22 31 58 63 78 84 Dustin Lubber Rusty Fluffy Morley Popeye 7 8 10 7 10 45.0 55.5 35.0 44.0 31.0 33.0 Step 1 SidSnameratingage 22 58 63 78 84 Dustin Rusty Fluffy Morley Popeye 7 10 7 10 45.0 35.0 44.0 31.0 33.0

42 42 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; Step 2 Sidsnameratingage 22 63 78 58 84 Dustin Fluffy Morley Rusty Popeye 7 10 45.0 44.0 31.0 35.0 33.0

43 43 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; Step 3 Sidsnameratingage 22 63 78 58 84 Dustin Fluffy Morley Rusty Popeye 7 10 45.0 44.0 31.0 35.0 33.0 Sidsnameratingage 22 63 78 Dustin Fluffy Morley 777777 45.0 44.0 31.0

44 44 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; Step 4 Sidsnameratingage 22 63 78 Dustin Fluffy Morley 777777 45.0 44.0 31.0 40 Final Answer:

45 45 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) For each subject, query its name and average task grade select subject_name, avg (grade) from grades group by subject_name

46 46 grades (pupil_name, subject_name, task_no, grade) For each subject, query its name and average task grade, where only passing grades (>=60) are included in the average select subject_name, avg (grade) from grades where grade>=60 group by subject_name For each subject, query its name and average task grade, where only subjects with passing average are displayed select subject_name, avg (grade) from grades group by subject_name Having avg(grade)>=60

47 47 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) What is wrong with the follwing? select grade from grades where grade>=60 group by subject_name We cannot select an attribute which is not grouped by (unless it is an aggregation operator)

48 48 Find name and age of oldest Sailor..? SELECT S.sname, MAX(S.age) FROM Sailors S Wrong! SELECT S.sname, MAX(S.age) FROM Sailors S GROUP BY S.sname Wrong: we don’t obtain what we want

49 49 Find name and age of oldest Sailor SELECT S.sname, S.age FROM Sailors S WHERE S.age >= ALL (SELECT S2.age FROM Sailors S2) SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX(S2.age) FROM Sailors S2) Right!! How else can this be done?

50 50 What does this return? SELECT B.bid, COUNT(*) FROM Boats B, Reserves R WHERE R.bid=B.bid and B.color=‘red’ GROUP BY B.bid For every reserved red boat : (id,number of reservations) What would happen if we put the condition about the color in the HAVING clause?

51 51 What would happen if we put the condition about the color in the HAVING clause? SELECT B.bid, COUNT(*) FROM Boats B, Reserves R WHERE R.bid=B.bid GROUP BY B.bid, B.color HAVING B.color=‘red’ It ’ s ok, but we also have to put the color in the grouping list!

52 52 What does this return? Can we move the condition in the HAVING to the WHERE? SELECT bname FROM Boats B, Reserves R WHERE R.bid=B.bid GROUP BY bid, bname HAVING count(DISTINCT day) <= 5 No! Aggregate functions are not allowed in WHERE Names of Boats that were not Reserved on more than 5 days

53 53 The Color for which there are the most boats..? SELECT color FROM Boats B GROUP BY color HAVING max(count(bid)) What is wrong with this? How would you fix it?

54 54 The Color for which there are the most boats SELECT color FROM Boats B GROUP BY color HAVING count(bid) >= ALL (SELECT count(bid) FROM Boats GROUP BY Color)

55 55 Aggregation Instead of Exists Aggregation can take the place of exists. What does this return? SELECT color FROM Boats B1 WHERE NOT EXISTS( SELECT * FROM Boats B2 WHERE B1.bid <> B2.bid AND B1.color=B2.color) The color of boats which have a unique color (no other boats with the same color)

56 56 Aggregation Instead of Exists SELECT color FROM Boats B1 GROUP BY color HAVING count(bid) = 1 Somewhat simpler …


Download ppt "1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,"

Similar presentations


Ads by Google