• recategorized by
10,330 views
32 32 votes

Suppose we have a database consisting of the following three relations.

  • $\text{FREQUENTS (student, parlor)}$ giving the parlors each student visits.
  • $\text{SERVES (parlor, ice-cream)}$ indicating what kind of ice-creams each parlor serves.
  • $\text{LIKES (student, ice-cream)}$ indicating what ice-creams each student likes.

(Assume that each student likes at least one ice-cream and frequents at least one parlor)

Express the following in SQL:

Print the students that frequent at least one parlor that serves some ice-cream that they like.

9 Answers

Best answer
75 75 votes
SELECT DISTINCT A.student FROM 
    FREQUENTS A, SERVES B, LIKES C
    WHERE
        A.parlor=B.parlor 
        AND
        B.ice-cream=C.ice-cream
        AND
        A.student=C.student;

OR

SELECT DISTINCT A.student FROM FREQUENTS A 
    WHERE
    parlor IN
        (SELECT parlor FROM SERVES B 
            WHERE B.ice-cream IN
                (SELECT ice-cream 
                FROM LIKES C 
                WHERE C.student = A.student));
• selected by
7 7 votes
SELECT DISTINCT STUDENT
FROM FREQUENT NATURAL JOIN SERVES NATURAL JOIN LIKES ;

 

2 2 votes
select distinct student
from FREQUENTS as f natural join SERVES as s
where icecream in (select icecream 
                   from LIKES as l
                   where f.student = l.student)

 

1 1 vote
Query using EXIST

SELECT Student from FREQUENTS F where EXIST ( SELECT * from SERVES S where F.PARLOR = S.PARLOR AND EXIST (SELECT * FROM LIKES L where S.ICECREAM = L.ICECREAM AND F.STUDENT= L.STUDENT))
0 0 votes

My Idea behind this Query:

Select those Ice-Creams liked by the student which are served at Parlour frequently visited by  the student.

 

SELECT DISTINCT student

FROM FREQUENTS AS F

WHERE EXISTS ( SELECT ice-cream

                               FROM LIKES AS L

WHERE EXISTS ( SELECT parlor

FROM SERVES AS L

WHERE L.parlor = F.parlor AND L.ice-cream = S.ice-cream 

)

);

0 0 votes

Select Distinct Student

From Frequent ⋈ Serves  ⋈  Like ;

OR

Select Distinct S.Student FROM 
    FREQUENTS P, SERVES Q, LIKES R
    WHERE
        P.Parlor=Q.Parlor 
        AND
        Q.ice-cream=R.ice-cream
        AND
        P.student=R.student;
Position:
Show:

Related questions

41 41 votes
10 answers 10 answers
17.0k
17.0k views
Kathleen asked Sep 26, 2014
17,014 views
Consider the following relational database schemes:COURSES (Cno, Name)PRE_REQ(Cno, Pre_Cno)COMPLETED (Student_no, Cno)COURSES gives the number and name of all the availab...
68 68 votes
6 answers 6 answers
30.8k
30.8k views
Kathleen asked Sep 26, 2014
30,789 views
Consider the following database relations containing the attributesBook_idSubject_Category_of_bookName_of_AuthorNationality_of_AuthorWith Book_id as the primary key.What ...
35 35 votes
4 answers 4 answers
10.0k
10.0k views
Kathleen asked Sep 26, 2014
10,009 views
Free disk space can be used to keep track of using a free list or a bit map. Disk addresses require $d$ bits. For a disk with $B$ blocks, $F$ of which are free, state the...
26 26 votes
4 answers 4 answers
9.6k
9.6k views
Arjun asked Aug 12, 2018
9,623 views
Let $R$ be a binary relation on $A = \{a, b, c, d, e, f, g, h\}$ represented by the following two component digraph. Find the smallest integers $m$ and $n$ such that $m <...