Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.

Similar presentations


Presentation on theme: "1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT."— Presentation transcript:

1 1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;

2 2 Rewriting Intersect Queries Using In SELECT 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 IN (SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’);

3 3 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.

4 4 Suppliers from A who supply All Parts from B snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A P2 pno B1 P2 P4 pno B2 P1 P2 P4 pno B3 S1 S4 sno A/B2 S1 sno A/B3 S1 S2 S3 S4 sno A/B1

5 5 Sailors who Reserved all Boats To find the Sailors who reserved all boats: (  sid,bid Reserves)/(  bid Boats) Division can be expressed using other relational algebra operators.

6 6 Division in SQL (1) SELECT S.sname 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

7 7 Division in SQL (2) SELECT S.sname FROM Sailors S WHERE NOT EXISTS((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid));

8 8 Aggregation

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

10 10 Some Examples SELECT COUNT(*) FROM Sailors S SELECT AVG(S.age) FROM Sailors S WHERE S.rating=10

11 11 Find name and age of oldest Sailor SELECT S.sname, MAX(S.age) FROM Sailors S SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX(S2.age) FROM Sailors S2) Wrong!! Right!!

12 12 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?

13 13 Basic SQL Query SELECT [Distinct] target-list FROM relation-list WHERE condition GROUP BY grouping-list HAVING group-condition; target-list: Fields appearing in grouping-list and aggregation operators group-condition: Can only constrain attributes appearing in grouping-list

14 14 Evaluation 1.Compute cross product of relations in FROM 2.Tuples failing WHERE are thrown away 3.Tuples are partitioned into groups by values of grouping-list attributes 4.The group-condition is applied to eliminate groups 5.One answer in generated for each group

15 15 Find Average Age for each Rating SELECT S.rating, AVG(S.age) FROM Sailors S GROUP BY S.rating

16 16 Find the number of Reservations of Each Red Boat SELECT B.bid, COUNT(*) as count FROM Boats B, Reserves R WHERE R.bid=B.bid and B.color=‘red’ GROUP BY B.bid What would happen if we put the condition about the color in the HAVING clause?

17 17 Age of Youngest Sailor that is over 18, for each Rating with at least 2 such Sailors SELECT S.rating, MIN(S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) > 1

18 18 Age of Youngest Sailor that is over 18, for each Rating with at least 2 Sailors (of any Age) SELECT S.rating, MIN(S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT(*) FROM Sailors S2 WHERE S.rating = S2.rating)

19 19 More Options

20 20 Sorting Results Results can be sorted using the ORDER BY clause Here are sailors who ordered boat 103, ordered by their names: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid and R.bid = 103 ORDER BY S.sname

21 21 Outer Join How can we find sailor names with their reservation numbers if we don’t want to lose information about sailors who did not reserve any boats?? SELECT S.sname, R.bid FROM Sailors S, Reserves R WHERE S.sid = R.sid(+) The (+) must follow column that we allow to be null.

22 22 Views A View is a query that looks like a table and can be used as a table. CREATE OR REPLACE VIEW SailorsBoats SELECT S.sname, B.bname, B.color FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid; SELECT sname FROM SailorBoats WHERE color = ‘red’;

23 23 Views For Restricting Access Suppose that we have a table: Grades(Login, Exercise, Grade) We would like a user to only be able to see his own grades. We create the following view and grant privileges to query the view (not the underlying table) CREATE OR REPLACE VIEW UserGrades SELECT * FROM Grades WHERE Login = User; Pseudo-column which is equal to the user name.

24 24 Views for Complex Queries Find those ratings for which the average age is the minimum over all ratings CREATE OR REPLACE VIEW AvgAgeRatings SELECT S.rating, AVG(S.age) as avgage FROM Sailors S WHERE S.rating; SELECT rating, avgage FROM AveAgeRatings WHERE avgage = (SELECT MIN(avgage) FROM AveAgeRatings)

25 25 Sub-queries in FROM, instead of Views SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG(S.age) as avgage FROM Sailors S GROUP BY S.rating) as Temp WHERE Temp.avgage = (SELECT MIN(Temp.avgage) FROM Temp)

26 26 Delete and Update

27 27 Deleting Tuples The basic form of a delete statement is: DELETE FROM TableName WHERE Condition;

28 28 Examples DELETE FROM Sailors WHERE rating < 3; DELETE FROM Sailors S1 WHERE S1.rating = (SELECT MIN(S2.rating) FROM Sailors S2) Delete Sailors with rating less than 3: Delete Sailors with the minimum rating:

29 29 Updating Tuples The basic form of an update statement is: UPDATE TableName SET Column1 = Value1, … ColumnN = ValueN WHERE Condition;

30 30 Example UPDATE Boats SET color = ‘red’, bname = ‘Voyager’ WHERE bid = 13; Update boat 13: color to red and name to voyager

31 31 Another Example UPDATE Sailors SET rating = (SELECT MAX(rating) FROM Sailors) WHERE sname = ‘Rusty’; Update rating of Rusty to be the maximum rating of any sailor Note: When updating with a subquery, the subquery must return one value only!


Download ppt "1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT."

Similar presentations


Ads by Google