GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 7(a)

Students liking a served ice cream

Database Management Systems · SQL & Queries · Full GATE CSE 1998 paper

GATE 1998, Q7(a) Descriptive
Report issue

SQL & Queries

Question 7(a), GATE 1998: Given FREQUENTS(student,parlor), SERVES(parlor,ice_cream), and LIKES(student,ice_cream), print students who frequent at least one parlor serving at least one ice cream they like. Each student likes an ice cream and frequents a parlor.
Question 7(a), GATE 1998: Given FREQUENTS(student,parlor), SERVES(parlor,ice_cream), and LIKES(student,ice_cream), print students who frequent at least one parlor serving at least one ice cream they like. Each student likes an ice cream and frequents a parlor.
Question 7(a), GATE 1998: Given FREQUENTS(student,parlor), SERVES(parlor,ice_cream), and LIKES(student,ice_cream), print students who frequent at least one parlor serving at least one ice cream they like. Each student likes an ice cream and frequents a parlor.
Tap to enlarge
Answer & solution

Answer: Join FREQUENTS to SERVES by parlor and LIKES by both student and ice cream; select distinct students. (no official key was published)

A qualifying student needs one witness consisting of a frequented parlor and a flavor both served there and liked by that same student. Match those three facts with a join:

SELECT DISTINCT f.student
FROM FREQUENTS AS f
JOIN SERVES AS s
  ON s.parlor = f.parlor
JOIN LIKES AS l
  ON l.student = f.student
 AND l.ice_cream = s.ice_cream;

The schema spells the attribute as “ice-cream”; ice_cream above is a SQL-friendly identifier for that same field. If the actual schema uses a hyphen, use the database's identifier quoting, for example l."ice-cream" in standard SQL. The query assumes the ordinary relational model with non-NULL identifying values.

The first join enumerates flavors served at every parlor a student frequents. The second retains only flavors liked by that student. Both equalities in the second join matter: omitting the student equality would incorrectly let somebody else's preference qualify the student. DISTINCT prints each qualifying student once even if several parlors or flavors witness qualification.

For example, if Ada frequents P, P serves vanilla, and Ada likes vanilla, the triple survives. If Bob also frequents P but only likes chocolate, Bob does not survive unless P serves chocolate as well. The request is existential, so it needs neither relational division nor a test that every frequented parlor serves every liked flavor. The assumptions that students have some likes and visits do not by themselves imply qualification.

Original paper, page 6