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.5k 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 Akash Kanase commented Dec 9, 2015 reply Follow flag First one is simplest to write ! 13 13 replyShare Tanushree commented Aug 17, 2016 reply Follow flag But Sir, how can u use correlation in 'IN' function? Only EXISTS and UNIQUE is used with correlation. 0 0 replyShare Arjun commented Aug 17, 2016 reply Follow flag Where is that condition given? 1 1 replyShare Tanushree commented Aug 26, 2016 reply Follow flag In my notes...but I got to know it's wrong....thanks for correcting me :) 1 1 replyShare suvasish pal commented Jul 21, 2017 reply Follow flag well explanation sir . 0 0 replyShare Tuhin Dutta commented Aug 9, 2017 reply Follow flag SELECT DISTINCT student FROM LIKES NATURAL JOIN SERVES NATURAL JOIN FREQUENTS; @Arjun sir won't this query work? 9 9 replyShare luc_Bloodstone commented Jul 15, 2020 reply Follow flag Can someone please explain me how the 2nd query will work, I didn't understand its working correctly. @Arjun 0 0 replyShare Gupta731 commented Jul 19, 2020 reply Follow flag Will my query work: select distinct F.student from Frequent F, Serves S where F.parlour=S.parlour and S.ice-cream=(Select ice-cream from Likes L, Frequents F where F.student=L.student); 0 0 replyShare yashpaneliya commented Oct 24, 2021 reply Follow flag SELECT DISTINCT F.STUDENT FROM FREQUENTS F WHERE F.PARLOUR IN (SELECT PARLOUR FROM SERVES JOIN LIKES) Is this correct?? 0 0 replyShare mohit7891 commented Oct 19, 2023 reply Follow flag @Gupta731 no it does not work because you are applying where condition incorrectly. S.ice-cream=(Select ice-cream from Likes L, Frequents F where F.student=L.student); the inner query return set of records and you are comparing with single record with ‘=’ that would not possible, it is possible with function IN/NOT IN. 0 0 replyShare 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.