Winter 2006Keller Cushing Ullman17–1 Subqueries Used mainly in FROM clauses and with quantifiers EXISTS and FORALL. Example: Subquery in FROM Find the.

Slides:



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

SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
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.
Fall 2001Arthur Keller – CS 18016–1 Schedule Nov. 20 (T) More OQL. u Read Sections Assignment 7 due (late ones by ). Nov. 22 (TH) Thanksgiving.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
Object-Orientation in Query Languages By: Toan Nguyen Class: CS 157A.
Fall 2001Arthur Keller – CS 18015–1 Schedule Nov. 15 (TH) Object Queries (OQL). u Read Sections 9.1. Project Part 6 due on Sunday night. Nov. 20 (T) More.
Object Oriented Database Management. Outline Motivation Embedding SQL in host language Object Data Model Persistent Programming Languages Object Query.
Winter 2002Arthur Keller – CS 18016–1 Schedule Today: Mar. 5 (T) u More ODL, OQL. u Read Sections 9.1. Assignment 7 due. Mar. 7 (TH) u More OQL. u Read.
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.
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
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.
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.
Winter 2006Keller, Ullman, Cushing18–1 Plan 1.Information integration: important new application that motivates what follows. 2.Semistructured data: a.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
1 Object-Oriented Database Languages Object Description Language Object Query Language.
1 Object-Oriented Database Languages Object Description Language Object Query Language.
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.
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
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 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
Relational Database Systems 1
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
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Design and Programming
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
Basic Operations Algebra of Bags
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CSC Database Management Systems
Object-Oriented DBMS’s
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Winter 2006Keller Cushing Ullman17–1 Subqueries Used mainly in FROM clauses and with quantifiers EXISTS and FORALL. Example: Subquery in FROM Find the manufacturers of the beers served at Joe's. SELECT DISTINCT b.manf FROM ( SELECT s.beer FROM Sells s WHERE s.bar.name = "Joe's Bar" ) b

Winter 2006Keller Cushing Ullman17–2 Quantifiers Boolean-valued expressions for use in WHERE -clauses. FOR ALL x IN : EXISTS x IN : The expression has value TRUE if the condition is true for all (resp., at least one) elements of the collection. Example Find all bars that sell some beer for more than $5. SELECT b.name FROM Bars b WHERE EXISTS s IN b.beersSold : s.price > 5.00 Problem How would you find the bars that only sold beers for more than $5?

Winter 2006Keller Cushing Ullman17–3 Example Find the bars such that the only beers they sell for more than $5 are manufactured by Pete’s. SELECT b.name FROM Bars b WHERE FOR ALL be IN ( SELECT s.beer FROM b.beersSold s WHERE s.price > 5.00 ) : be.manf = "Pete's"

Winter 2006Keller Cushing Ullman17–4 Extraction of Collection Elements a)A collection with a single member: Extract the member with ELEMENT. Example Find the price Joe charges for Bud and put the result in a variable p. p = ELEMENT( SELECT s.price FROM Sells s WHERE s.bar.name = "Joe's Bar" AND s.beer.name = "Bud" )

Winter 2006Keller Cushing Ullman17–5 b)Extracting all elements of a collection, one at a time: 1. Turn the collection into a list. 2. Extract elements of a list with [i]. Example Print Joe's menu, in order of price, with beers of the same price listed alphabetically. L = SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's Bar" ORDER BY s.price, s.beer.name; printf("Beer\tPrice\n\n"); for(i=1; i<=COUNT(L); i++) printf("%s\t%f\n", L[i].name, L[i].price );

Winter 2006Keller Cushing Ullman17–6 Aggregation The five operators avg, min, max, sum, count apply to any collection, as long as the operators make sense for the element type. Example Find the average price of beer at Joe’s. x = AVG( SELECT s.price FROM Sells s WHERE s.bar.name = "Joe's Bar" ); Note coercion: result of SELECT is technically a bag of 1-field structs, which is identified with the bag of the values of that field.

Winter 2006Keller Cushing Ullman17–7 Grouping Recall SQL grouping, for example: SELECT bar, AVG(price) FROM Sells GROUP BY bar; Is the bar value the “name” of the group, or the common value for the bar component of all tuples in the group? In SQL it doesn't matter, but in OQL, you can create groups from the values of any function(s), not just attributes. u Thus, groups are identified by common values, not “name.” u Example: group by first letter of bar names (method needed).

Winter 2006Keller Cushing Ullman17–8 Outline of OQL Group-By Collection defined by FROM, WHERE Collection with function values and partition Output collection Group by values of function(s) Terms from SELECT clause

Winter 2006Keller Cushing Ullman17–9 Example Find the average price of beer at each bar. SELECT barName, avgPrice: AVG( SELECT p.s.price FROM partition p ) FROM Sells s GROUP BY barName: s.bar.name 1.Initial collection = Sells.  But technically, it is a bag of structs of the form Struct(s: s1 )  Where s1 is a Sell object. Note, the lone field is named s ; in general, there are fields for all of the “typical objects” in the FROM clause.

Winter 2006Keller Cushing Ullman17–10 2.Intermediate collection:  One function: s.bar.name maps Sell objects s to the value of the name of the bar referred to by s.  Collection is a set of structs of type: Struct{barName: string, partition: Set< Struct{s: Sell} > } u For example: Struct(barName = "Joe's Bar", partition = { s 1,…,s n } )  where s 1,…,s n are all the structs with one field, named s, whose value is one of the Sell objects that represent Joe's Bar selling some beer.

Winter 2006Keller Cushing Ullman17–11 3.Output collection: consists of beer-average price pairs, one for each struct in the intermediate collection. u Type of structures in the output: Struct{barName: string, avgPrice: real}  Note that in the subquery of the SELECT clause: SELECT barName, avgPrice: AVG( SELECT p.s.price FROM partition p ) We let p range over all structs in partition. Each of these structs contains a single field named s and has a Sell object as its value. Thus, p.s.price extracts the price from one of the Sell objects. u Typical output struct: Struct(barName = "Joe's Bar", avgPrice = 2.83)

Winter 2006Keller Cushing Ullman17–12 Another, Less Typical Example Find, for each beer, the number of bars that charge a “low” price (≤ 2.00) and a “high” price (≥ 4.00) for that beer. Strategy: group by three things: 1. The beer name, 2. A boolean function that is true iff the price is low. 3. A boolean function that is true iff the price is high.

Winter 2006Keller Cushing Ullman17–13 The Query SELECT beerName, low, high, count: COUNT(partition) FROM Beers b, b.soldBy s GROUP BY beerName: b.name, low: s.price <= 2.00, high: s.price >= Initial collection: Pairs (b, s), where b is a Beer object, and s is a Sell object representing the sale of that beer at some bar. u Type of collection members: Struct{b: Beer, s: Sell}

Winter 2006Keller Cushing Ullman17–14 2.Intermediate collection: Quadruples consisting of a beer name, booleans telling whether this group is for high, low, or neither prices for that beer, and the partition for that group.  The partition is a set of structs of the type: Struct{b: Beer, s: Sell} A typical value: Struct(b: "Bud" object, s: a Sell object involving Bud )

Winter 2006Keller Cushing Ullman17–15 u Type of quadruples in the intermediate collection: Struct{ beerName: string, low: boolean, high: boolean, partition: Set<Struct{ b: Beer, s: Sell }> } Typical structs in intermediate collection: beerNamelowhighpartition BudTRUEFALSES low BudFALSETRUES high BudFALSEFALSES mid ………… where S low, S high, and S mid are the sets of beer-sells pairs (b, s) where the beer is Bud and s has, respectively, a low (≤ 2.00), high (≥ 4.00), and medium (between 2.00 and 4.00) price. Note the partition with low = high = TRUE must be empty and will not appear.

Winter 2006Keller Cushing Ullman17–16 3.Output collection: The first three components of each group's struct are copied to the output, and the last ( partition ) is counted. The result: beerNamelowhighcount BudTRUEFALSE27 BudFALSETRUE14 BudFALSEFALSE36 …………