in Databases retagged by
3,627 views
26 votes
26 votes
Suppose we have a database consisting of the following three relations:
$$\begin{array}{|c|c|} \hline \text {FREQUENTS} & \text {(CUSTOMER, HOTEL)} \\\hline \text {SERVES} &  \text{(HOTEL, SNACKS)}\\\hline \text{LIKES} & \text{(CUSTOMER, SNACKS)}\\\hline \end{array}$$
The first indicates the hotels each customer visits, the second tells which snacks each hotel serves and last indicates which snacks are liked by each customer. Express the following query in relational algebra:

Print the hotels the serve the snack that customer Rama likes.
in Databases retagged by
3.6k views

3 Answers

42 votes
42 votes
Best answer
OPTIMIZED ANSWER

$\Pi_{hotel} \Big( ( \sigma _{customer = ``Rama"}{(LIKES)} )  \bowtie{SERVES} \Big)$
edited by

13 Comments

This one is a better answer as it selects the tuples from the LIKES table before performing the join. Hence the number of comparison required in the join are reduced.
6
6
Isn't we should also take natural join of FREQUENTS with (σcustomer=‘‘Rama"(LIKES))⋈SERVES) as there might be a case where a hotel serves the snacks liked by a customer but that customer doesn't visit that hotel...
7
7
Why not

$\Pi_{hotel} \Big( ( \sigma _{customer = ``Rama"}{(LIKES)} ) \times {SERVES} \Big)$

Moreover if we do join then Frequents also need to be included in join condition
0
0
is the given join is natural???
0
0
1
1
But I think when no clause is given to the join, then it it simple cartesian product
0
0
ya, when no common attribute it is simple cross product but here they have common attribute SNACKS
0
0
got it.
0
0

@Gurdeep Saini

then check this query https://gateoverflow.in/19838/gate1997-76-a

why here used cross product?

0
0

@pallaviamu FREQUENTS table is not to be joined as the question doesn't mention the clause of printing only those hotels that Rama visits.

0
0

I think we have to consider only those Hotels where Rama visits. As the question states – “Print the hotels the serve the snack that customer Rama likes.” She should be called customer of only those hotels where she visits.

0
0
yes the answer should be :-

 πHotel (FREQUENTS⋈((σcustomer=‘‘Rama"(LIKES))⋈SERVES))

this should be the answer i think .
–1
–1

@jatinmittal199510  The thing is here we will select the customer with name rama so after the natural join  of seves and likes if there is no customer with name rama in likes then he will not be printed  and also if he likes the same item from different they also will be printed

0
0
13 votes
13 votes

πHotelcustomer = "RAMA"(LIKES (Natural Join)Serves))

1 comment

edited by
see my comment this is not correct i think
–1
–1
0 votes
0 votes
$\Pi_{Hotel} (\;Serves\;\bowtie\;(\sigma_{Customer=Rama}\; Likes)\;)$

Related questions