Suppose we have a database consisting of the following three relations.
(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.
This is my Attempt of your Query Pratik
from frequents F
where NOT EXIST
( Select Student, Parlor, Ice-cream
from F NATURAL JOIN Likes L
select Student, Parlor, Ice-cream
from Serves NATURAL JOIN L);
SELECT DISTINCT A.student FROM
FREQUENTS A, SERVES B, LIKES C
SELECT DISTINCT A.student FROM FREQUENTS A
(SELECT parlor FROM SERVES B
WHERE B.ice-cream IN
FROM LIKES C
WHERE C.student = A.student));
Will my query work:
select distinct F.student from Frequent F, Serves S
and S.ice-cream=(Select ice-cream from Likes L, Frequents F
For those who face a problem in understanding the 2nd solution, they should visit the correlated subquery concept first.
SELECT DISTINCT F.STUDENT FROM FREQUENTS F WHERE F.PARLOUR IN (SELECT PARLOUR FROM SERVES JOIN LIKES)
Is this correct??
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)
SELECT DISTINCT STUDENT
FROM FREQUENT NATURAL JOIN SERVES NATURAL JOIN LIKES ;