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

Slides:



Advertisements
Similar presentations
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
Advertisements

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Subqueries Example Find the name of the producer of ‘Star Wars’.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation Source: slides by Jeffrey Ullman.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Chapter 5 Notes. P. 189: Sets, Bags, and Lists To understand the distinction between sets, bags, and lists, remember that a set has unordered elements,
Databases 1 Second lecture.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
1 Lecture 6 Introduction to SQL part 4 Slides from
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
CS 440 Database Management Systems
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Design and Programming
CPSC-310 Database Systems
IST 210: Organization of Data
Operators Expression Trees Bag Model of Data
CPSC-310 Database Systems
IT 244 Database Management System
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

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

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 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 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 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 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 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 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 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 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 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 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 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 Example: GROUP-BY/HAVING SELECT name FROM Beers WHERE manf IN ( SELECT manf FROM Beers GROUP-BY manf HAVING count(manf) = 1);

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 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 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 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 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 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 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 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 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 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 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 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 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 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 The Extended Algebra δ = eliminate duplicates from bags. τ = sort tuples. γ = grouping and aggregation. Outerjoin : avoids “dangling tuples” = tuples that do not join with anything.

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

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

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 Example: Sorting R = ( AB ) τ B (R) = [(5,2), (1,2), (3,4)]

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 Example: Aggregation R = ( AB ) SUM(A) = 7 COUNT(A) = 3 MAX(B) = 4 AVG(B) = 3

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 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 Example: Grouping/Aggregation R = ( ABC ) γ A,B,AVG(C)->X (R) = ?? First, group R by A and B : ABC Then, average C within groups: ABX

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 Example: Outerjoin R = ( AB )S = ( BC ) (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S =ABC NULL NULL67

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

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 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 Example: Aggregation uFrom Sells(bar, beer, price), find the average price of Bud: SELECT AVG(price) FROM Sells WHERE beer = ’Bud’;

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Example: Default Values CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

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

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

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 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 Deletion uTo delete tuples satisfying a condition from some relation: DELETE FROM WHERE ;

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

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

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 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 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 Updates uTo change certain attributes in certain tuples of a relation: UPDATE SET WHERE ;

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

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