CS 440 Database Management Systems

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,
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.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
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.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
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.
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.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
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
CPSC-310 Database Systems
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
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Chapter 3 Introduction to SQL(3)
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Example: R3 := R1 × R2 R3( A, R1.B, R2.B, C )‏ R1( A, B )‏ R2( B, C )‏ R3( A, R1.B, R2.B, C )‏
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
Database Models Relational Model
CPSC-310 Database Systems
CS 405G: Introduction to Database Systems
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
Basic Operations Algebra of Bags
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
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

CS 440 Database Management Systems Lecture 3: Review of Relational Model and SQL

Announcements You may find the chapters related to each lecture in the schedule page of the course Web site. Check out the textbook website for practice problems with solutions: pages.cs.wisc.edu/~dbbook/

Example schema Beers(name, manf) Bars(name, addr, license) Drinkers( name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

Aggregation functions Compute some value based on the values of an attribute. Example functions: Count, Sum, Avg, Min, Max Each RDBMS may define additional functions. Example: Using Bars(name, addr, license), find the number of bars. Select Count(name) From Bars;

Aggregation functions Using Distinct, aggregation functions ignore duplicates. Example: Using Likes(drinker, beer), find the number of drinkers who like Bud Lite. Select Count( Distinct drinker) From Likes Where beer =‘Bud Lite’;

Aggregation functions Generally, aggregation functions do not consider NULL values. Select Count(price) From Sells Where bar=‘Joe Bar’; Select Count(beer) Select Count(*) The number of priced beers sold by Joe Bar. The number of beers sold by Joe Bar. The number of beers sold by Joe Bar.

Aggregation functions over groups We want to aggregate values for groups of tuples. Example: Using Sells(bar, beer, price) find the minimum price of each beer. Group tuples in Sells based on beer. Compute Min over the prices in each group of tuples.

Group by Example: Using Sells(bar, beer, price) find the minimum price of each beer. Select beer, Min(price) As minprice From Sells Group By beer; optional

Group by Select beer, Min(price),bar You may use multiple attributes for grouping. The attributes in the Select clause are either aggregated values or attributes in the Group By clause. Select beer, Min(price),bar From Sells Group By beer; Exceptions in some RDBMS, e.g., MySQL 5.7. Generally, Group By does not sort the groups. There are exceptions, e.g., older versions of MySQL, but do not trust them! error

Grouping attributes from different relations. Example: Using Likes(drinker, beer) and Sells(bar, beer, price), for each drinker find the minimum price of every beer he/she likes. Select drinker, beer, Min(price) As minprice From Likes, Sells Where Likes.beer = Sells.beer Group By drinker, beer;

Filtering groups We may filter out some groups using their attributes’ values. Select beer, Min(price) As minprice From Sells Where bar=‘Red Lion’ or bar=‘Big Horse’ Group By beer;

Filtering groups based on aggregated values Example: Using Sells(bar, beer, price), find the minimum price of each beer whose maximum price is less than 11. Select beer, Min(price) As minprice From Sells Where Max(price) < 11 Group By beer error

Having clause We use Having clauses to filter out groups based on their aggregated values. Select beer, Min(price) As minprice From Sells Group By beer Having Max(price) < 11

Having clause We may use aggregated values over attributes other than the ones in the Group By clause. Example: Using Sells(bar, beer, price), find the minimum price of each beer sold in more than three bars. Select beer, Min(price) As minprice From Sells Group By beer Having Count(bar) > 3

Having clause may act as a Where clause Example: Using Sells(bar, beer, price), find the minimum price of Bud or beers whose maximum price is less than 11. Select beer, Min(price) From Sells Group By beer Having (Max(price) < 11) Or (beer=‘Bud’) It works only for the attributes in the Group By clause. Having (Max(price) < 11) Or (bar=‘Red Lion’) error

Sorting the output Example: Using Sells(bar, beer, price) find the minimum price of each beer whose maximum price is at least 15 and sort the results according to beers’ names. Select beer, Min(price) As minprice From Sells Group By beer Having Max(price) >= 15 Order By beer;

Sorting the output One may use Desc to change the sort order. Previous example in descending order of beers’ names: Select beer, Min(price) As minprice From Sells Group By beer Having Max(price) >= 15 Order By beer Desc; You may use Order By without Group By and Having. Example: Using Sells(bar, beer, price), provide a list of beer prices sorted by bars’ and beers’ names. Select bar, beer, price Order By bar, beer;

Review problems: people betting on OSU football games Out(game, outcome) Bets(who, outcome, game, amt) Some games have not been played yet, e.g., Arizona. game outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120

Problem 1 List the completed games that nobody bet on. game USC W UCLA outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 game

Problem 1 From Out) Except From Bets) (Select Game List the completed games that nobody bet on. (Select Game From Out) Except From Bets)

Problem 2 Who bet the most money on a single game? game USC W UCLA L outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 who amt Kevin 210

Problem 2 Who bet the most money on a single game? Select Who, Amt From Bets Where Amt >= All (Select Amt From Bets)

Problem 3 List the games that all bettors agree on. game USC W UCLA L outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 game Stanford Arizona UO

Problem 3 List the games that all bettors agree on. (Select game From Bets) Except (Select Bets1.game From Bets Bets1, Bets Bets2 Where (Bets1.game = Bets2.game) And (Bets1.outcome <> Bets2.outcome))

Problem 4 For each game, the number of people betting on OSU to win and the number betting on OSU to lose. game outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 game outcome num Stanford W 1 UO L UCLA USC Arizona

Problem 4 For each game, the number of people betting on OSU to win and the number betting on OSU to lose. Select game, outcome, Count(who) As num From Bets Group By game, outcome

Problem 5 Find the people who have made two or more bets on OSU to lose. game outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 who Kevin

Problem 5 Find the people who have made two or more bets on OSU to lose. Select who From Bets Where outcome = ‘L’ Group By who Having Count(outcome) >= 2

Problem 6 Who bet the most money overall? game USC W UCLA L Stanford outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 who sumAmt John 450

Problem 6 Who bet the most money overall? Select who, Sum(amt) As sumAmt From Bets Group By who Having Sum(amt) >= ALL (Select Sum(amt) Group By who)

Problem 7 Who has bet on every game? game USC W UCLA L Stanford who outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 Who

Problem 7 Who has bet on every game? Create View AllGames As (Select game From Out) Union (Select game From Bets) Select who From Bets Group By who Having Count(Distinct game) = (Select Count(Distinct game) From AllGames)

Problem 8 What games have won the most money for the people who bet on OSU to win? game outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 game USC

Problem 8 What games have won the most money for the people who bet on OSU to win? Create View Success-Win As (Select Bets.game, Sum(Bets.amt) As SumAmt From Bets, Out Where out.game = Bets.game And Bets.outcome = ‘W’ And Out.outcome = ‘W’ Group By Game) Select Distinct game From Success-Win Where SumAmt >= All (Select SumAmt From Success-Win)

Problem 9 List the people who won some money so far. game USC W UCLA L outcome USC W UCLA L Stanford who outcome game amt John W USC 200 UCLA 100 L Arizona 150 Kevin UO 210 50 Stanford 120 who John Kevin

Problem 9 List the people who won some money so far. Create View Success As (Select who, Sum(amt) As pAmt From Bets, Out Where Out.game = Bets.game And Bets.outcome = Out.outcome Group By who) Create View Failure As (Select who, Sum(amt) As nAmt Where Out.game = Bets.game And Bets.outcome <> Out.outcome

Problem 9 Union List the people who won some money so far. (Select Success.who From Success, Failure Where Success.who = Failure.who And Success.pAmt > Failure.nAmt ) Union (Select who From Success Where who not in From Failure) )

Data manipulation: insertion Inserting new tuple(s) into a relation. Insert into R(A1,…,An) Values (a1,…,an); Example: insert tuple (‘Big Horse’,’Bud’,3) in Sells. Insert into Sells(bar, beer, price) Values (‘Big Horse’,‘Bud’,3); The input of Insert can be the result of another query. Insert into Beers(name) Select beer From Sells;

Data manipulation: deletion Removing tuple(s) from a relation. Delete From R Where R.A = ‘a’; Remove all tuples from relation R. Delete From R; Example: John does not go to Old Horse anymore. Delete From Frequents Where drinker = ‘John’ and bar = ‘Old Horse’;

Data manipulation: update Updating the values of given attributes in certain tuples of a relation. Update R Set R.A = a Where R.B = b; Old Horse has changed the price of Bud to 3. Update Sells Set price = 3 Where bar = ‘Old Horse’ And beer = ‘Bud’;

Is SQL enough? Using IsParent(parent,child), find all descendants of a given person. It is proved that it is not possible to write this query in SQL that we have discussed so far. It (relational algebra/ calculus) express first order logic. We need more expressive to describe recursion. There are generally three methods to make SQL stronger to express these queries.