Solutions to practice problems on queries

1. (a) List the bars that serve a beer that Joe likes. (i) tuple calculus:

CSE 232

Fall, 2000

(ii) domain calculus:

fb : bar j 9s 2 serves9l 2 likes (s(bar) = b(bar) ^ s(beer) = l(beer) ^ l(drinker) = Joe)g: fb : bar j 9c : beer(serves(b; c) ^ likes(c; Joe))g:
bar (serves 1 drinker=Joe(likes)):

(iii) relational algebra: (iv) SQL:

AND likes.drinker = \Joe" (v) QBE:

select serves.bar from serves, likes where serves.beer = likes.beer
serves bar beer likes drinker beer b c Joe c answer bar I. b

(b) List the drinkers that frequent at least one bar that serves a beer they like. (i) tuple calculus:

fd : drinker j 9f 2 frequents9s 2 serves9l 2 likes (f (drinker) = d(drinker) ^ f (bar) = s(bar) ^s(beer) = l(beer) ^ l(drinker) = d(drinker))g
1

beer and likes.bar (c) List the drinkers that frequent only bars that serve some beer that they like. d fd : drinker j 8f 2 frequents f (drinker) = d(drinker) ! 9s 2 serves9l 2 likes(s(bar) = f (bar)^ s(beer) = l(beer) ^ l(drinker) = d(drinker))g (ii) domain calculus: (iii) relational algebra: fd : drinker j 8b frequents(d. c))]g drinker frequents drinker. serves.(ii) domain calculus: fd : drinker j 9b9c frequents(d. (Assume each drinker likes at least one beer and frequents at least one bar. c) ^ likes(d. c)]g (iii) relational algebra: (iv) SQL: drinker (frequents 1 serves 1 likes) and serves. b) ^ serves(b. c) ^ likes(d.) (i) tuple calculus: frequents drinker bar d b likes drinker beer d c serves bar beer b c answer drinker I.drinker (v) QBE: select frequents.drinker from frequents.drinker = frequents. b) ! 9c(serves(b.bar = serves. likes where frequents.beer = likes.bar (serves 1 likes)]: drinker (frequents) 2 .

bar not in (select bar from serves. likes where serves.beer = likes.drinker = f. d 3 .drinker)) (v) QBE: Stage I ( nd compatible drinker-bar pairs): likes serves bar beer b c compatible drinker I.beer and likes.(iv) SQL: select drinker from frequents where drinker not in (select f. d drinker beer d c bar b Stage II ( nd drinkers who frequent some incompatible bar): frequents drinker bar d b compatible drinker bar : d b bad drinker I. d Stage III ( nd answer by taking the complement): frequents drinker bar bad drinker d : d answer drinker I.drinker from frequents f where f.

actor (film) title ( director=K (film)): 4 . a)g: actor ( director=K (film)): select actor from lm where director = K (iv) QBE: lm title director actor K P. K. (i) domain calculus: fa : actor j 8t 9x (film(t.actor director=K (film))] Also. (i) domain calculus: (ii) relational algebra: (iii) SQL: fa : actor j 9t film(t. This is just the complement of (b).(d) List the drinkers that frequent no bar that serves a beer that they like. (a) List the actors cast in at least one movie by Kurosawa (here and below. K. title. K stands for Kurosawa). 2. K. x)) ! film(t. a)g: (ii) relational algebra: actor (film) actor title ( director=K (film)) 1 actor (film) ( title. (b) List the actors cast in every movie by Kurosawa.

actor not in (select actor from lm where title = g.(iii) SQL: select actor from lm where actor not in (select f. a (c) List the actors cast only in movies by Kurosawa. a) ! d = K ]g actor (film) actor ( director6=K (film)): 5 .title )) (iv) QBE: Stage I lm title director actor a t K : t a I.director = K and f.actor from lm f g where g. d. bad-actors actor a lm title director actor a bad-actors actor : a answer actor I. (i) domain calculus: (ii) relational algebra: (iii) SQL: Stage II fa : actor j 8t8d film(t.

g. d. a1 a2 6 .title I. a1 ) ^ film(t. bad-actors actor a lm title director actor a bad-actors actor : a answer actor I. a (d) List all pairs of actors who act together in at least one movie.actor from lm f g where f.(iv) QBE: select actor from lm where actor not in (select actor from lm where director 6= K ) lm title director actor 6= K a Stage I I.actor2 actor!actor1 (film) 1 actor!actor2 (film)] (iii) SQL: (iv) QBE: lm title director actor t a1 t a2 answer actor1 actor2 select f.actor. (i) domain calculus: Stage II fa1 : actor1 . d. a2))g (ii) relational algebra: actor1 .title = g. a2 : actor2 j 9t9d(film(t.

bad actor1 actor2 a1 a2 .actor !actor .actor not in (select actor from lm where title = x. g. y. a0 > such that a acts in every movie where a0 acts.actor from lm f g where <f.actor (q): 0 0 select f. d.actor. d. This is expressed by: actor (film) actor!actor ( actor (film)) actor.actor from lm x y where y. a2 )]g (ii) relational algebra: Consider rst the query q asking for pairs of actors < a.actor> not in Q where Q is: (iv) QBE: select x. f. (i) domain calculus: fa1 : actor1 . a1) $ film(t.title) lm title director actor a1 t a2 t a1 : 7 Stage I I.actor.(e) List all pairs of actors cast in exactly the same movies.actor> not in Q and <g.actor actor (film) actor!actor (film) 0 0 0 film actor!actor ( actor (film))] 0 The given query is now expressed by: q1 (iii) SQL: actor. g. a1 : actor2 j 8t8d film(t.actor.actor.

director not in (select director from lm where actor = g. (i) domain calculus: Stage II fd : director j 8a 9t9e(film(t. bad-directors director d .actor (film) actor (film): select director from lm where director not in (select f. a1 a2 (f) List the directors such that every actor is cast in one of his/her lms.lm title director actor a1 a2 bad actor1 actor2 : a1 a2 : a2 a1 answer actor1 actor2 I. d. e.actor )) (iv) QBE: Stage I lm title director actor d a d a : 8 I.director from lm f g where f. a)) ! 9tfilm(t. a)]g (ii) relational algebra: (iii) SQL: director.

essentially. (b) False (note that this says. One way to make the evaluation more e cient is to rewrite the query as director title. that existential quanti ca tion distributes over conjunction). 5.director (movie) 1 title ( theater6="Cove" (schedule))] Stage II R A B S A B This cuts down the size of relations participating in the join and avoids building up an unnecessarily large intermediate result (resulting from the join). 4. A counterexample is: 0 0 0 1 (c) False. (a) True.lm title director actor d bad-directors director : d answer director I. d 3. Same counterxample as for (b). Consider the following rspj algebra query (R is a relation with attributes ABC ): AC AB (R) 1 BC (R)] 1 A AC (R) 1 CB (R)]: (a) The tableau corresponding to the query is: A a a1 a a2 a 9 B b1 b1 b2 b3 C c1 c c2 c2 c . The proof uses distributivity of existential quanti cation over disjunction.

The minimized tableau is: A B C 5 8 c1 a1 8 c 5 c A corresponding algebra query is: AC ( A=5 B=8 (R)) 1 C ( B=8 (R)): 10 .(c) for the query: AC AB (R) 1 BC ( B=8 (R))] 1 A AC ( A=5 (R)) 1 CB (R)]: The corresponding tableau is: A B C 5 8 c1 a1 8 c 5 b1 c2 a2 b2 c2 5 c The last two rows can be mapped to the rst.(b). So the minimized tableau is: A B C a b1 c1 a 1 b1 c a c (c) An rspj algebra query corresponding to the minimized tableau is: AC ( AB (R) 1 BC (R)): (d) Redo (a).(b) There is a homomorphism of the tableau mapping the 2nd and 3rd rows to the rst.

C ! Ag yields (after 11 . C ! Ag yields: A a a CHASE (T ) = a a a Eliminating duplicate rows from CHASE A B C a b c1 a b2 c a b c and the corresponding rspj query is AB (R) 1 AC (R): B C b c1 b c1 b c1 b2 c b c (T ) yields the minimal tableau: (ii)The tableau T corresponding to q is eliminating duplicate rows): A B C a b1 5 a1 b 5 8 b c2 8 b2 c a b c Chasing with respect to = fAC ! B. B ! C. B ! C.6. (i) The tableau T corresponding to q is A B C a b1 c1 a 1 b c1 a 2 b c2 a 2 b2 c a b c Chasing with respect to = fAC ! B.

8 b 5 CHASE (T ) = 8 b2 c A B C This tablueau is minimal. and the corresponding rspj query is AB ( A=8 ( C =5 (R))) 1 AC ( A=8 (R)): a b c 12 .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.