SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections 6.3-6.4. Next u Modifications, Schemas, Views. u Read.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

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.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
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.
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.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
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
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
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.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Databases 1 Second lecture.
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.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
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
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
Computational Biology
CS 480: Database Systems Lecture 17 February 22, 2013.
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
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
Instructor: Mohamed Eltabakh
CMSC-461 Database Management Systems
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:

SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read Sections After that u Constraints. u Read Sections ,

SCUHolliday - coen 1787–2 Union, Intersection, Difference “(subquery) UNION (subquery)” produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and set difference.  But: in Oracle set difference is MINUS, not EXCEPT. Example Find the drinkers and beers such that the drinker likes the beer and frequents a bar that serves it. Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar);

SCUHolliday - coen 1787–3 Forcing Set Semantics Default for select-from-where is bag; default for union, intersection, and difference is set. Force set semantics with DISTINCT after SELECT. Find the different prices charged for beers. Sells(bar, beer, price) SELECT DISTINCT price FROM Sells;

SCUHolliday - coen 1787–4 Forcing Bag Semantics Force bag semantics with ALL In the case of set operations, duplicates are automatically eliminated. To retain duplicates, use union all, intersect all, and except all.

SCUHolliday - coen 1787–5 More on Bags There may be duplicate tuples in the results of SQL queries. To determine the number of duplicate tuples in the result. 1. If there are c1 copies of a tuple t1 in r and t1 satisfies the predicate P, then there will be c1 copies of t1 in select * from r where P 2.If there are c1 copies of a tuple t1 in r, then there will be c1 copies of select A from t1 in select A from r 3.If there are c1 copies of a tuple t1 in r and c2 copies of t2 in s, then there will be c1* c2 copies of the tuple t1.t2 in select * from r, s

SCUHolliday - coen 1787–6 How many tuples? select * r= from s where C = 3 select B from r select B s= from r, s select Bselect A from r, sfrom r,s where C = 3where A=C AB 1a 2a C 2 3 3

SCUHolliday - coen 1787–7 Forcing Set Semantics with DISTINCT select distinct B r= from r select distinct B from r, s s= select distinct C from s AB 1a 2a C 2 3 3

SCUHolliday - coen 1787–8 Exercise: Use set operations to- Find all customers who have both a loan and an account. Find all customers who have an account but no loan. Depositor = (customer-name, account#) Borrower = (customer-name, loan#)

SCUHolliday - coen 1787–9 Answers (select customer-name from Depositor) intersect (select customer-name from Borrower) (select customer-name from Depositor) except (select customer-name from Borrower)

SCUHolliday - coen 1787–10 Join-Based Expressions A number of forms are provided. Can be used either stand-alone (in place of a select-from- where) or to define a relation in the FROM -clause. R NATURAL JOIN S R JOIN S ON condition(cross product +  ) e.g., condition: R.B=S.B R CROSS JOIN S(cross product) R OUTER JOIN S Outerjoin can be modified by: 1. Optional NATURAL in front. 2. Optional ON condition at end. 3. Optional LEFT, RIGHT, or FULL (default) before OUTER.  LEFT = pad (with NULL ) dangling tuples of R only; RIGHT = pad dangling tuples of S only.

SCUHolliday - coen 1787–11 Join Exercise R =S = What is R natural join S? ABC a10135 b10324 b10465 DB Smith101 Jones102 Johnson103 ABCD a10135Smith b10324Johnson

SCUHolliday - coen 1787–12 Outer Join Exercise R =S = What is R natural left outer join S? ABC a10135 b10324 b10465 DB Smith101 Jones102 Johnson103 ABCD a10135Smith b10324Johnson b Null--

SCUHolliday - coen 1787–13 Outer Join Exercise R =S = What is R natural right outer join S? ABC a10135 b10324 b10465 DB Smith101 Jones102 Johnson103 ABCD a10135Smith b10324Johnson --null--102Jones

SCUHolliday - coen 1787–14 Full Outer Join Exercise R =S = What is R natural full outer join S? ABC a10135 b10324 b10465 DB Smith101 Jones102 Johnson103 ABCD a10135Smith b10324Johnson B null-- 102Jones

SCUHolliday - coen 1787–15 Outer Join Exercise R =S = What is R left outer join S on R.B=S.B? ABC a10135 b10324 b10465 DB Smith101 Jones102 Johnson103 ABBCD a101 35Smith b103 24Johnson b Null--

SCUHolliday - coen 1787–16 Aggregations Sum, avg, min, max, and count apply to attributes (columns). Also, count(*) applies to tuples. Use these in lists following SELECT. (also having ) Output has only one tuple Example Find the average price of Bud. Sells(bar, beer, price) SELECT AVG(price) FROM Sells WHERE beer = 'Bud';

SCUHolliday - coen 1787–17 Eliminating Duplicates Before Aggregation Find the number of different prices at which Bud is sold. Sells(bar, beer, price) SELECT COUNT(DISTINCT price) FROM Sells WHERE beer = 'Bud'; DISTINCT may be used in any aggregation, but typically only makes sense with COUNT.

SCUHolliday - coen 1787–18 Example Using AVG Find the average account balance at the Oakland branch. Account = (branch-name, account#, balance) select avg(balance) from Account where branch-name = "Oakland“ What does the result look like?

SCUHolliday - coen 1787–19 Example Using AVG Find the average account balance at the Oakland branch of accounts over $3000. Account = (branch-name, account#, balance) select avg(balance) from Account where branch-name = "Oakland“ and balance>3000 B-nameAcc#Balance Oakland Oakland Oakland

SCUHolliday - coen 1787–20 Grouping Follow select-from-where by GROUP BY and a list of attributes. The relation that is the result of the FROM and WHERE clauses is grouped according to the values of these attributes, and aggregations take place only within a group. Example: Find the average sales price for each beer. Sells(bar, beer, price) SELECT beer, AVG(price) FROM Sells GROUP BY beer;

SCUHolliday - coen 1787–21 Avg sales price for each beer SELECT beer, AVG(price) FROM Sells GROUP BY beer; BarBeerPrice Joe’sBud2.00 Joe’sMiller3.00 Kelly’sBud3.00 O’SheaMiller5.00 Joe’sBud2.00 Kelly’sBud3.00 Joe’sMiller3.00 O’SheaMiller5.00 Bud2.00 Bud3.00 Miller3.00 Miller5.00 Bud2.50 Miller4.00

SCUHolliday - coen 1787–22 Example Find, for each drinker, the average price of Bud at the bars they frequent. Sells(bar, beer, price) Frequents(drinker, bar) SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = 'Bud' AND Frequents.bar = Sells.bar GROUP BY drinker;

SCUHolliday - coen 1787–23 When Grouping… Note: grouping occurs after the cross product in FROM and condition in WHERE is performed. When rows (tuples) are grouped, one line of output is produced for each group.

SCUHolliday - coen 1787–24 Restriction on SELECT Lists With Aggregation If any aggregation is used, then each element of a SELECT clause must either be aggregated or appear in a group-by clause. Example The following might seem a tempting way to find the bar that sells Bud the cheapest: Sells(bar, beer, price) SELECT bar, MIN(price) FROM Sells WHERE beer = 'Bud'; But it is illegal in most SQL implementations. What would the result be if it was legal?

SCUHolliday - coen 1787–25 HAVING Clauses HAVING clauses are selections on groups, just as WHERE clauses are selections on tuples. Condition can use the tuple variables or relations in the FROM and their attributes, just like the WHERE can. u But the tuple variables range only over the group. u And the attribute better make sense within a group; i.e., be one of the grouping attributes.

SCUHolliday - coen 1787–26 Example Find the average price of those beers that are either served in at least 3 bars or manufactured by Anheuser-Busch. Beers(name, manf) Sells(bar, beer, price) SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(*) >= 3 OR beer IN ( SELECT name FROM Beers WHERE manf = 'Anheuser-Busch' );

SCUHolliday - coen 1787–27 Before we go on.. How do we find the bar that sells Bud the cheapest? Sells(bar, beer, price) Select bar From Sells Where beer = ‘Bud’ and price = (select min(price) from Sells where beer = ‘Bud’ )

SCUHolliday - coen 1787–28 Alternatively SELECT bar FROM Sells WHERE beer = ‘Bud’ and price <= ALL( SELECT price FROM Sells WHERE beer = ‘Bud’);

SCUHolliday - coen 1787–29 Database Modifications So far, we have looked at queries that ask about the current state of the database (instance). We use similar syntax to make changes to the database. Modification = insert + delete + update.

SCUHolliday - coen 1787–30 DB Insert Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of values, associating values with attributes in the order the attributes were declared. u Forget the order? List the attributes as arguments of the relation. Example Likes(drinker, beer) Insert the fact that Sally likes Bud. INSERT INTO Likes(drinker, beer) VALUES('Sally', 'Bud');

SCUHolliday - coen 1787–31 Insertion of the Result of a Query INSERT INTO relation (subquery). Example Create a (unary) table of all Sally's buddies, i.e., the people who frequent bars that Sally also frequents. Frequents(drinker, bar) CREATE TABLE Buddies( name char(30) ); INSERT INTO Buddies (SELECT DISTINCT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = 'Sally' AND d2.drinker <> 'Sally' AND d1.bar = d2.bar );

SCUHolliday - coen 1787–32 Deletion DELETE FROM relation WHERE condition. Deletes all tuples satisfying the condition from the named relation. Example Sally no longer likes Bud. Likes(drinker, beer) DELETE FROM Likes WHERE drinker = 'Sally' AND beer = 'Bud'; Example Make the Likes relation empty. DELETE FROM Likes;

SCUHolliday - coen 1787–33 Example Delete all beers for which there is another beer by the same manufacturer. Beers(name, manf) DELETE FROM Beers b WHERE EXISTS (SELECT name FROM Beers WHERE manf = b.manf AND name <> b.name ); Note alias for relation from which deletion occurs. Subquery evaluated once for each row of b

SCUHolliday - coen 1787–34 Semantics is tricky. If A.B. makes Bud and BudLite (only), does deletion of Bud make BudLite not satisfy the condition? SQL semantics: all conditions in modifications must be evaluated by the system before any mods due to that mod command occur. u In Bud/Budlite example, we would first identify both beers a targets, and then delete both.

SCUHolliday - coen 1787–35 More on Delete Oracle 8i does not allow complex conditions in the where clause. You can only delete from one table at a time. Delete all accounts at every branch located in Fremont. delete from Account where branch-name in (select branch-name from Branch where branch-city="Fremont" )

SCUHolliday - coen 1787–36 Updates UPDATE relation SET list of assignments WHERE condition. Example Drinker Fred's phone number is Drinkers(name, addr, phone) UPDATE Drinkers SET phone = ' ' WHERE name = 'Fred';

SCUHolliday - coen 1787–37 Example - Update Make $4 the maximum price for beer. Updates many tuples at once. Sells(bar, beer, price) UPDATE Sells SET price = 4.00 WHERE price > 4.00;

SCUHolliday - coen 1787–38 Review/Quiz select branch-name, avg(balance) fromAccount where account# > 5000 group by branch-name having avg(balance) > 2000 B-nameAcc#Balance Main Main Oak Oak

SCUHolliday - coen 1787–39 Answer If both a where clause and a having command are present, the where clause is done first. B-nameAVG(Balance) Main3000