GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 12(a.ii)

Intercity shipment triples

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

GATE 1991, Q12(a.ii) Descriptive
Report issue

SQL & Queries

Question 12(a.ii), GATE 1991: SUPPLIER(SCODE,SNAME,CITY), PART(PCODE,PNAME,PDESC,CITY), PROJECTS(PRCODE,PRNAME,CITY), SPPR(SCODE,PCODE,PRCODE,QTY). SPPR records shipments of parts by suppliers to projects. Write SQL returning distinct triples (supplier city, part code, project city), excluding equal cities.
Question 12(a.ii), GATE 1991: SUPPLIER(SCODE,SNAME,CITY), PART(PCODE,PNAME,PDESC,CITY), PROJECTS(PRCODE,PRNAME,CITY), SPPR(SCODE,PCODE,PRCODE,QTY). SPPR records shipments of parts by suppliers to projects. Write SQL returning distinct triples (supplier city, part code, project city), excluding equal cities.
Question 12(a.ii), GATE 1991: SUPPLIER(SCODE,SNAME,CITY), PART(PCODE,PNAME,PDESC,CITY), PROJECTS(PRCODE,PRNAME,CITY), SPPR(SCODE,PCODE,PRCODE,QTY). SPPR records shipments of parts by suppliers to projects. Write SQL returning distinct triples (supplier city, part code, project city), excluding equal cities.
Tap to enlarge
Answer & solution

Answer: Join shipments to suppliers and projects, filter S.CITY <> P.CITY, and select the distinct three requested attributes. (no official key was published)

A row must describe an actual shipment rather than an arbitrary supplier/project combination. Use SPPR as the central relation and join the endpoint relations by their code keys. The supplier city is the first component; the destination project's city is the third.

SELECT DISTINCT S.CITY AS SUPPLIER_CITY,
                X.PCODE,
                P.CITY AS PROJECT_CITY
FROM SPPR AS X
JOIN SUPPLIER AS S ON S.SCODE = X.SCODE
JOIN PROJECTS AS P ON P.PRCODE = X.PRCODE
WHERE S.CITY <> P.CITY;

The predicate removes local shipments, and DISTINCT collapses repeated triples caused by different suppliers, projects or shipment rows. PART.CITY is irrelevant: it is neither endpoint requested in the result. Aliases make the two CITY columns distinguishable. Classical relational problems normally assume nonnull attributes. In SQL, if a city is NULL, the inequality is UNKNOWN and the row is excluded; a different policy for unknown cities would require an explicit requirement and predicate.

Original paper, page 7