Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.

Similar presentations


Presentation on theme: "1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update."— Presentation transcript:

1 1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

2 2 Our Running Example uAll our SQL queries will be based on the following database schema. wUnderline indicates key attributes. Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

3 3 Subqueries uA parenthesized SELECT-FROM-WHERE statement (subquery ) can be used as a value in a number of places, including FROM and WHERE clauses. uExample: in place of a relation in the FROM clause, we can use a subquery and then query its result. wMust use a tuple-variable to name tuples of the result.

4 4 Example: Subquery in FROM uFind the beers liked by at least one person who frequents Joe’s Bar. SELECT beer FROM Likes, (SELECT drinker FROM Frequents WHERE bar = ’Joe’’s Bar’)JD WHERE Likes.drinker = JD.drinker; Drinkers who frequent Joe’s Bar

5 5 Example: Subquery in WHERE uFind the beers liked by at least one person who frequents Joe’s Bar. SELECT beer FROM Likes WHERE Likes.drinker IN (SELECT drinker FROM Frequents WHERE bar = ’Joe’’s Bar’); Drinkers who frequent Joe’s Bar

6 6 Subqueries That Return One Tuple uIf a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. wUsually, the tuple has one component. wA run-time error occurs if there is no tuple or more than one tuple.

7 7 Example: Single-Tuple Subquery uUsing Sells(bar, beer, price), find the bars that serve Miller for the same price Joe charges for Bud. uTwo queries would surely work: 1.Find the price Joe charges for Bud. 2.Find the bars that serve Miller at that price.

8 8 Query + Subquery Solution SELECT bar FROM Sells WHERE beer = ’Miller’ AND price = (SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’); The price at which Joe sells Bud

9 9 The IN Operator u IN ( ) is true if and only if the tuple is a member of the relation produced by the subquery. wOpposite: NOT IN ( ). uIN-expressions can appear in WHERE clauses.

10 10 Example: IN uUsing Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. SELECT * FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker = ’Fred’); The set of beers Fred likes

11 11 The Exists Operator uEXISTS( ) is true if and only if the subquery result is not empty. uExample: From Beers(name, manf), find those beers that are the unique beer by their manufacturer.

12 12 Example: EXISTS SELECT name FROM Beers b1 WHERE NOT EXISTS ( SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); Set of beers with the same manf as b1, but not the same beer Notice scope rule: manf refers to closest nested FROM with a relation having that attribute. Notice the SQL “not equals” operator

13 13 Example: COUNT/GROUP BY SELECT name FROM Beers B, (SELECT count(*) as cnt, manf FROM Beers GROUP BY manf) CM WHERE CM.cnt = 1 AND CM.B.manf = CM.manf ;

14 14 Example: GROUP-BY/HAVING SELECT name FROM Beers WHERE manf IN ( SELECT manf FROM Beers GROUP-BY manf HAVING count(manf) = 1);

15 15 The Operator ANY uThink of it as “some” ux = ANY( ) is a boolean condition that is true iff x equals at least one tuple in the subquery result. w= could be any comparison operator. uExample: x >= ANY( ) means x is not the uniquely smallest tuple produced by the subquery.

16 16 The Operator ALL ux <> ALL( ) is true iff for every tuple t in the relation, x is not equal to t. wThat is, x is not in the subquery result. u<> can be any comparison operator. uExample: x >= ALL( ) means there is no tuple larger than x in the subquery result.

17 17 Example: ALL uFrom Sells(bar, beer, price), find the beer(s) sold for the highest price. SELECT beer FROM Sells WHERE price >= ALL(SELECT price FROM Sells); price from the outer Sells must not be less than any price. SELECT beer FROM Sells WHERE price = (SELECT MAX(price) FROM Sells) SELECT beer FROM Sells WHERE price = (SELECT MAX(price) FROM Sells) OR

18 18 Union, Intersection, and Difference uUnion, intersection, and difference of relations are expressed by the following forms, each involving subqueries: w( ) UNION ( ) w( ) INTERSECT ( ) w( ) EXCEPT ( )

19 19 Example: Intersection uUsing Likes(drinker, beer), Sells(bar, beer, price), and Frequents(drinker, bar), find the drinkers and beers such that: 1.The drinker likes the beer, and 2.The drinker frequents at least one bar that sells the beer.

20 20 Solution (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar ); The drinker frequents a bar that sells the beer. Notice trick: subquery is really a stored table.

21 21 Bag Semantics uAlthough the SELECT-FROM-WHERE statement uses bag semantics, the default for union, intersection, and difference is set semantics. wThat is, duplicates are eliminated as the operation is applied.

22 22 Motivation: Efficiency uWhen doing projection, it is easier to avoid eliminating duplicates. wJust work tuple-at-a-time. uFor intersection or difference, it is most efficient to sort the relations first. wAt that point you may as well eliminate the duplicates anyway.

23 23 Controlling Duplicate Elimination uForce the result to be a set by SELECT DISTINCT... uForce the result to be a bag (i.e., don’t eliminate duplicates) by ALL, as in... UNION ALL...

24 24 Example: DISTINCT uFrom Sells(bar, beer, price), find all the different prices charged for beers: SELECT DISTINCT price FROM Sells; uNotice that without DISTINCT, each price would be listed as many times as there were bar/beer pairs at that price.

25 25 Example: ALL uUsing relations Frequents(drinker, bar) and Likes(drinker, beer): (SELECT drinker FROM Frequents) EXCEPT ALL (SELECT drinker FROM Likes); uLists drinkers who frequent more bars than they like beers, and does so as many times as the difference of those counts.

26 26 Join Expressions uSQL provides several versions of (bag) joins. uThese expressions can be stand-alone queries or used in place of relations in a FROM clause.

27 27 Products and Natural Joins uNatural join: R NATURAL JOIN S; uProduct: R CROSS JOIN S; uExample: Likes NATURAL JOIN Sells; uRelations can be parenthesized subqueries, as well.

28 28 Theta Join uR JOIN S ON uExample: using Drinkers(name, addr) and Frequents(drinker, bar): Drinkers JOIN Frequents ON name = drinker; gives us all (d, a, d, b) quadruples such that drinker d lives at address a and frequents bar b.

29 29 The Extended Algebra δ = eliminate duplicates from bags. τ = sort tuples. γ = grouping and aggregation. Outerjoin : avoids “dangling tuples” = tuples that do not join with anything.

30 30 Duplicate Elimination  R1 := δ (R2). uR1 consists of one copy of each tuple that appears in R2 one or more times.

31 31 Example: Duplicate Elimination R = (AB ) 12 34 12 δ (R) =AB 12 34

32 32 Sorting  R1 := τ L (R2). wL is a list of some of the attributes of R2. uR1 is the list of tuples of R2 sorted first on the value of the first attribute on L, then on the second attribute of L, and so on. wBreak ties arbitrarily.  τ is the only operator whose result is neither a set nor a bag (why?).

33 33 Example: Sorting R = ( AB ) 12 34 52 τ B (R) = [(5,2), (1,2), (3,4)]

34 34 Aggregation Operators uAggregation operators are not operators of relational algebra. uRather, they apply to entire columns of a table and produce a single result. uThe most important examples: SUM, AVG, COUNT, MIN, and MAX.

35 35 Example: Aggregation R = ( AB ) 13 34 32 SUM(A) = 7 COUNT(A) = 3 MAX(B) = 4 AVG(B) = 3

36 36 Grouping Operator  R1 := γ L (R2). L is a list of elements that are either: 1.Individual (grouping ) attributes. 2.AGG(A ), where AGG is one of the aggregation operators and A is an attribute. An arrow and a new attribute name renames the component.

37 37 Applying γ L (R) uGroup R according to all the grouping attributes on list L. wThat is: form one group for each distinct list of values for those attributes in R. uWithin each group, compute AGG(A ) for each aggregation on list L. uResult has one tuple for each group: 1.The grouping attributes and 2. Their group’s aggregations.

38 38 Example: Grouping/Aggregation R = ( ABC ) 123 456 125 γ A,B,AVG(C)->X (R) = ?? First, group R by A and B : ABC 123 125 456 Then, average C within groups: ABX 124 456

39 39 Outerjoin  Suppose we join R ⋈ C S. uA tuple of R that has no tuple of S with which it joins is said to be dangling. wSimilarly for a tuple of S. uOuterjoin preserves dangling tuples by padding them NULL.

40 40 Example: Outerjoin R = ( AB )S = ( BC ) 1223 4567 (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S =ABC 123 45NULL NULL67

41 41 Now --- Back to SQL Each Operation Has a SQL Equivalent

42 42 Outerjoins uR OUTER JOIN S is the core of an outerjoin expression. It is modified by: 1.Optional NATURAL in front of OUTER. 2.Optional ON after JOIN. 3.Optional LEFT, RIGHT, or FULL before OUTER. uLEFT = pad dangling tuples of R only. uRIGHT = pad dangling tuples of S only. uFULL = pad both. Only one of these

43 43 Aggregations uSUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column. uAlso, COUNT(*) counts the number of tuples.

44 44 Example: Aggregation uFrom Sells(bar, beer, price), find the average price of Bud: SELECT AVG(price) FROM Sells WHERE beer = ’Bud’;

45 45 Eliminating Duplicates in an Aggregation uUse DISTINCT inside an aggregation. uExample: find the number of different prices charged for Bud: SELECT COUNT(DISTINCT price) FROM Sells WHERE beer = ’Bud’;

46 46 NULL’s Ignored in Aggregation uNULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column. uBut if there are no non-NULL values in a column, then the result of the aggregation is NULL. wException: COUNT of an empty set is 0.

47 47 Example: Effect of NULL’s SELECT count(*) FROM Sells WHERE beer = ’Bud’; SELECT count(price) FROM Sells WHERE beer = ’Bud’; The number of bars that sell Bud. The number of bars that sell Bud at a known price.

48 48 Grouping uWe may follow a SELECT-FROM- WHERE expression by GROUP BY and a list of attributes. uThe relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group.

49 49 GROUP BY Table output by WHERE clause: - Divide rows into groups based on subset of attributes; - All members of a group agree on those attributes Each group can be described by a single row in a table with attributes limited to: -Attributes all group members share (listed in GROUP BY clause) -Aggregates over group group GROUP BY attributes aabbcccccdddaabbcccccddd

50 50 Example: Grouping uFrom Sells(bar, beer, price), find the average price for each beer: SELECT beer, AVG(price) FROM Sells GROUP BY beer; beerAVG(price) Bud2.33…

51 51 Example: Grouping uFrom Sells(bar, beer, price) and Frequents(drinker, bar), find for each drinker the average price of Bud at the bars they frequent: SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = ’Bud’ AND Frequents.bar = Sells.bar GROUP BY drinker; Compute all drinker-bar- price triples for Bud. Then group them by drinker.

52 52 Restriction on SELECT Lists With Aggregation uIf any aggregation is used, then each element of the SELECT list must be either: 1.Aggregated, or 2.An attribute on the GROUP BY list.

53 53 Illegal Query Example uYou might think you could find the bar that sells Bud the cheapest by: SELECT bar, MIN(price) FROM Sells WHERE beer = ’Bud’; uBut this query is illegal in SQL.

54 54 HAVING Clauses uHAVING may follow a GROUP BY clause. uIf so, the condition applies to each group, and groups not satisfying the condition are eliminated  HAVING condition constructed from attributes of GROUP BY list and aggregates of attributes not in list

55 55 Example: HAVING uFrom Sells(bar, beer, price) and Beers(name, manf), find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s.

56 56 Solution SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); Beers manu- factured by Pete’s. Beer groups with at least 3 non-NULL bars and also beer groups where the manufacturer is Pete’s.

57 57 Example uOutput the name and address of all seniors on the Dean’s List SELECT S.Name, S.Address FROM Student S, Transcript T WHERE S.StudId = T.StudId AND S.Status = ‘senior’ GROUP BY HAVING AVG (T.Grade) > 3.5 AND SUM (T.Credit) > 90 S.StudId -- wrong S.Name, S.Address -- right

58 58 ORDER BY Clause uCauses rows to be output in a specified order SELECT T.StudId, COUNT (*) AS NumCrs, AVG (T.Grade) AS CumGpa FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5 ORDER BY DESC CumGpa, ASC StudId

59 59 Restrictions on GROUP BY/HAVING/ORDER BY SELECT attr list, aggregates FROM relation list WHERE where cond GROUP BY group list HAVING group cond ORDER BY ordered attr list attr list must be subset of group list group cond must evaluate to a single value for each group ordered attr list must be a subset of attr list

60 60 Database Modifications uA modification command does not return a result (as a query does), but changes the database in some way. uThree kinds of modifications: 1.Insert a tuple or tuples. 2.Delete a tuple or tuples. 3.Update the value(s) of an existing tuple or tuples.

61 61 Insertion uTo insert a single tuple: INSERT INTO VALUES ( ); uExample: add to Likes(drinker, beer) the fact that Sally likes Bud. INSERT INTO Likes VALUES(’Sally’, ’Bud’);

62 62 Specifying Attributes in INSERT uWe may add to the relation name a list of attributes. uTwo reasons to do so: 1.We forget the standard order of attributes for the relation. 2.We don’t have values for all attributes, and we want the system to fill in missing components with NULL or a default value.

63 63 Example: Specifying Attributes uAnother way to add the fact that Sally likes Bud to Likes(drinker, beer): INSERT INTO Likes(beer, drinker) VALUES(’Bud’, ’Sally’);

64 64 Adding Default Values uIn a CREATE TABLE statement, we can follow an attribute by DEFAULT and a value. uWhen an inserted tuple has no value for that attribute, the default will be used.

65 65 Example: Default Values CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

66 66 Example: Default Values INSERT INTO Drinkers(name) VALUES(’Sally’); Resulting tuple: Sally123 Sesame StNULL nameaddress phone

67 67 Inserting Many Tuples uWe may insert the entire result of a query into a relation, using the form: INSERT INTO ( );

68 68 Example: Insert a Subquery uUsing Frequents(drinker, bar), enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents.

69 69 Solution INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar ); Pairs of Drinker tuples where the first is for Sally, the second is for someone else, and the bars are the same. The other drinker

70 70 Deletion uTo delete tuples satisfying a condition from some relation: DELETE FROM WHERE ;

71 71 Example: Deletion uDelete from Likes(drinker, beer) the fact that Sally likes Bud: DELETE FROM Likes WHERE drinker = ’Sally’ AND beer = ’Bud’;

72 72 Example: Delete all Tuples uMake the relation Likes empty: DELETE FROM Likes; uNote no WHERE clause needed.

73 73 Example: Delete Some Tuples uDelete from Beers(name, manf) all beers for which there is another beer by the same manufacturer. DELETE FROM Beers b WHERE EXISTS ( SELECT name FROM Beers WHERE manf = b.manf AND name <> b.name); Beers with the same manufacturer and a different name from the name of the beer represented by tuple b.

74 74 Semantics of Deletion --- (1) uSuppose Anheuser-Busch makes only Bud and Bud Lite. uSuppose we come to the tuple b for Bud first. uThe subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. uNow, when b is the tuple for Bud Lite, do we delete that tuple too?

75 75 Semantics of Deletion --- (2) uAnswer: we do delete Bud Lite as well. uThe reason is that deletion proceeds in two stages: 1.Mark all tuples for which the WHERE condition is satisfied. 2.Delete the marked tuples.

76 76 Updates uTo change certain attributes in certain tuples of a relation: UPDATE SET WHERE ;

77 77 Example: Update uChange drinker Fred’s phone number to 555-1212: UPDATE Drinkers SET phone = ’555-1212’ WHERE name = ’Fred’;

78 78 Example: Update Several Tuples uMake $4 the maximum price for beer: UPDATE Sells SET price = 4.00 WHERE price > 4.00;


Download ppt "1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update."

Similar presentations


Ads by Google