You are on page 1of 12

# 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)):

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

## fd : drinker j 9b9c frequents(d; b) ^ serves(b; c) ^ likes(d; c)]g

(iii) relational algebra: (iv) SQL:
drinker (frequents 1 serves 1 likes)

## select frequents.drinker from frequents, serves, likes where frequents.bar = serves.bar

(c) List the drinkers that frequent only bars that serve some beer that they like. (Assume each drinker likes at least one beer and frequents at least one bar.) (i) tuple calculus:

## serves bar beer b c answer drinker I. 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; b) ! 9c(serves(b; c) ^ likes(d; c))]g

drinker frequents drinker;bar (serves 1 likes)]:

drinker (frequents)

(iv) SQL:

select drinker from frequents where drinker not in (select f.drinker from frequents f where f.bar not in (select bar from serves, likes where serves.beer = likes.beer
and likes.drinker = f.drinker)) (v) QBE:

## Stage I ( nd compatible drinker-bar pairs):

likes serves bar beer b c compatible drinker I. 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. d
3

(d) List the drinkers that frequent no bar that serves a beer that they like. This is just the complement of (b). 2. (a) List the actors cast in at least one movie by Kurosawa (here and below, K stands for Kurosawa). (i) domain calculus: (ii) relational algebra: (iii) SQL:

## fa : actor j 9t film(t; K; a)g:

actor ( director=K (film)):

## select actor from lm where director = K

(iv) QBE: lm title director actor K P. (b) List the actors cast in every movie by Kurosawa. (i) domain calculus:

## fa : actor j 8t 9x (film(t; K; x)) ! film(t; K; a)g:

(ii) relational algebra:
actor (film) actor title ( director=K (film)) 1 actor (film) ( title;actor director=K (film))]

Also,
title;actor (film) title ( director=K (film)):

(iii) SQL:

select actor from lm where actor not in (select f.actor from lm f g where g.director = K and f.actor not in (select actor from lm where title = g.title ))
(iv) QBE:

Stage I

## lm title director actor a t K : t a

I.

bad-actors actor a

lm title director actor a bad-actors actor : a answer actor I. a (c) List the actors cast only in movies by Kurosawa. (i) domain calculus: (ii) relational algebra: (iii) SQL:

Stage II

## fa : actor j 8t8d film(t; d; a) ! d = K ]g

actor (film) actor ( director6=K (film)):

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

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. (i) domain calculus:

Stage II

## fa1 : actor1 ; a2 : actor2 j 9t9d(film(t; d; a1 ) ^ film(t; d; a2))g

(ii) relational algebra:
actor1 ;actor2 actor!actor1 (film) 1 actor!actor2 (film)]

(iii) SQL:

## select f.actor, g.actor from lm f g where f.title = g.title

I.

a1

a2

(e) List all pairs of actors cast in exactly the same movies. (i) domain calculus:

## fa1 : actor1 ; a1 : actor2 j 8t8d film(t; d; a1) \$ film(t; d; a2 )]g

(ii) relational algebra: Consider rst the query q asking for pairs of actors < a; a0 > such that a acts in every movie where a0 acts. This is expressed by:
actor (film) actor!actor ( actor (film)) actor;actor actor (film) actor!actor (film)
0 0 0

film

0

q1
(iii) SQL:

## actor;actor !actor ;actor (q):

0 0

select f.actor, g.actor from lm f g where <f.actor, g.actor> not in Q and <g.actor, f.actor> not in Q
where Q is:

(iv) QBE:

select x.actor, y.actor from lm x y where y.actor not in (select actor from lm where title = x.title)
lm title director actor a1 t a2 t a1 : 7

Stage I

I.

## bad actor1 actor2 a1 a2

lm title director actor a1 a2 bad actor1 actor2 : a1 a2 : a2 a1 answer actor1 actor2 I. a1 a2 (f) List the directors such that every actor is cast in one of his/her lms. (i) domain calculus:

Stage II

## fd : director j 8a 9t9e(film(t; e; a)) ! 9tfilm(t; d; a)]g

(ii) relational algebra: (iii) SQL:
director;actor (film) actor (film):

select director from lm where director not in (select f.director from lm f g where f.director not in (select director from lm where actor = g.actor ))
(iv) QBE:

Stage I

## lm title director actor d a d a : 8

I.

bad-directors director d

lm title director actor d bad-directors director : d answer director I. d 3. (a) True. The proof uses distributivity of existential quanti cation over disjunction. (b) False (note that this says, essentially, that existential quanti ca tion distributes over conjunction). A counterexample is: 0 0 0 1 (c) False. Same counterxample as for (b). 4. One way to make the evaluation more e cient is to rewrite the query as
director title;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). 5. 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

(b) There is a homomorphism of the tableau mapping the 2nd and 3rd rows to the rst. 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),(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. 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

## 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; B ! C; 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):

## 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; C ! Ag yields (after

11

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