NivaarExam PrepOfficial exam papers ↗

25-Comp-B3 Data Bases and File Systems · December 2013

Question 5 of 8: SQL queries — Suppliers/Parts/Catalog

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

98-Comp-B3, Data Bases & File Systems — National Exams, December 2013. 3 hours, closed book (calculators permitted). Candidates were instructed to answer five questions: one of Questions 1/2, one of Questions 3/4, and three of Questions 5–8 — only those five are marked. All 8 questions are answered below for completeness (this is a study resource covering the full syllabus).

Reference texts: Silberschatz, Korth & Sudarshan, Database System Concepts (7th ed.) — ER modelling, normal forms, transactions and serializability; Ramakrishnan & Gehrke, Database Management Systems (3rd ed.) — B+-tree indexing, SQL, and relational algebra.

Question 5: SQL queries — Suppliers/Parts/Catalog (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. Suppliers(sid, sname, address), Parts(pid, pname, color), Catalog(sid, pid, cost) with Catalog.sid/pid foreign keys into Suppliers/Parts.

(a) pnames of parts for which there is some supplier — a straightforward join/semijoin: any part appearing in Catalog has a supplier.

SELECT DISTINCT P.pname
FROM   Parts P
WHERE  EXISTS (SELECT * FROM Catalog C WHERE C.pid = P.pid);

(b) suppliers who supply every red part — a universal ("for all") quantification, expressed by double negation: no red part exists that this supplier does NOT supply.

SELECT S.sname
FROM   Suppliers S
WHERE  NOT EXISTS (
         SELECT * FROM Parts P
         WHERE  P.color = 'red'
         AND    NOT EXISTS (
                  SELECT * FROM Catalog C
                  WHERE  C.sid = S.sid AND C.pid = P.pid));

(c) sids who charge more for some part than that part's average cost — a correlated subquery re-evaluated per Catalog row, averaging over only the suppliers who actually supply that same part.

SELECT DISTINCT C1.sid
FROM   Catalog C1
WHERE  C1.cost > (
         SELECT AVG(C2.cost) FROM Catalog C2
         WHERE  C2.pid = C1.pid);

(d) for every supplier supplying both a green and a red part, the name and price of their most expensive part — filter suppliers by the two EXISTS conditions, then a per-supplier correlated MAX.

SELECT S.sname,
       (SELECT MAX(C3.cost) FROM Catalog C3 WHERE C3.sid = S.sid) AS maxprice
FROM   Suppliers S
WHERE  EXISTS (SELECT * FROM Catalog C1, Parts P1
               WHERE C1.sid = S.sid AND C1.pid = P1.pid AND P1.color = 'green')
AND    EXISTS (SELECT * FROM Catalog C2, Parts P2
               WHERE C2.sid = S.sid AND C2.pid = P2.pid AND P2.color = 'red');
Sample-data check — Question 5
QueryResult on the sample dataset
(a)Bolt, Nail, Screw, Nut
(b)Yosemite, BoltCorp
(c)sids 2, 3
(d)(Yosemite, 20), (BoltCorp, 25)