Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 440 Database Management Systems

Similar presentations


Presentation on theme: "CS 440 Database Management Systems"— Presentation transcript:

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

2 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/

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

4 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;

5 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’;

6 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.

7 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.

8 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

9 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

10 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;

11 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;

12 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

13 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

14 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

15 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

16 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;

17 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;

18 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

19 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

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

21 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

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

23 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

24 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))

25 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

26 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

27 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

28 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

29 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

30 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)

31 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

32 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)

33 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

34 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)

35 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

36 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

37 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) )

38 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;

39 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’;

40 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’;

41 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.


Download ppt "CS 440 Database Management Systems"

Similar presentations


Ads by Google