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. Databases gate1998 databases sql descriptive + – Kathleen 10.3k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Pratik Gawali commented Oct 13, 2018 reply Follow flag What would be the query if the question was: Print the students that frequent at least one parlor that serves all ice-creams that they like. 0 0 replyShare Nitesh Singh 2 commented Oct 31, 2018 reply Follow flag This is my Attempt of your Query Pratik Select Student from frequents F where NOT EXIST ( Select Student, Parlor, Ice-cream from F NATURAL JOIN Likes L EXCEPT select Student, Parlor, Ice-cream from Serves NATURAL JOIN L); 2 2 replyShare Please log in or register to add a comment.
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)); Arjun answered Jul 10, 2015 • selected Nov 17, 2015 by Pooja Palod Arjun comment Share Follow See all 13 Comments 13 13 Comments reply Show 10 previous comments ash_khola commented Jul 13, 2024 reply Follow flag Sir can't we directly join 3 tables and just put one condition that frequents.student = likes.student ? 0 0 replyShare ꧁༒☬ĿọŗԀ 🆂🅷🅸🆅🅰☬༒꧂ commented Aug 11, 2024 reply Follow flag @ash_khola after natural join u can directly select distinct students.. see this ans https://gateoverflow.in/1721/gate-cse-1998-question-7-a?show=364808#a364808 0 0 replyShare ash_khola commented Aug 11, 2024 reply Follow flag Yes thats what i was looking for, thanks. 1 1 replyShare Please log in or register to add a comment.
7 7 votes SELECT DISTINCT STUDENT FROM FREQUENT NATURAL JOIN SERVES NATURAL JOIN LIKES ; HitechGa answered Oct 21, 2021 HitechGa comment Share Follow 0 reply Please log in or register to add a comment.
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) 0xprateek answered Apr 7, 2021 0xprateek comment Share Follow 0 reply Please log in or register to add a comment.
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)) Prateek K answered Jan 20, 2018 Prateek K comment Share Follow 0 reply Please log in or register to add a comment.
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 studentFROM FREQUENTS AS FWHERE EXISTS ( SELECT ice-cream FROM LIKES AS LWHERE EXISTS ( SELECT parlorFROM SERVES AS LWHERE L.parlor = F.parlor AND L.ice-cream = S.ice-cream )); justanotherguy answered Jul 2, 2025 justanotherguy comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Select Distinct StudentFrom Frequent ⋈ Serves ⋈ Like ;ORSelect 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; akshay_123 answered Aug 22, 2025 akshay_123 comment Share Follow 0 reply Please log in or register to add a comment.