Categories
Uncategorized

DBMS Query

Ex: 5.2 Ramakrishna, gehrke

Consider the following schema:

Suppliers(sid: integer, sname: string, address: string)

Parts(pid: integer, pname: string, color: string)

Catalog(sid: integer, pid: integer, cost: real)

The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL:

  • Find the pnames of parts for which there is some supplier.

             SELECT DISTINCT P.pname

             FROM Parts P, Catalog C

             WHERE P.pid = C.pid

  • Find the snames of suppliers who supply every part.

             SELECT S.sname FROM Suppliers S

            WHERE NOT EXISTS ( ( SELECT P.pid FROM Parts P )

            EXCEPT( SELECT C.pid FROM Catalog C

                           WHERE C.sid = S.sid ) )

  • Find the snames of suppliers who supply every red part.

           SELECT S.sname FROM Suppliers S

          WHERE NOT EXISTS ( ( SELECT P.pid FROM Parts P

                                                  WHERE P.color = ‘Red’ )

                                                  EXCEPT ( SELECT C.pid FROM Parts P2, Catalog C

                                                  WHERE C.sid = S.sid AND C.pid = P.pid AND P.color = ‘Red’ ) )

  • Find the pnames of parts supplied by Acme Widget Suppliers and no one else.

           SELECT P.pname FROM Parts P, Catalog C, Suppliers S

          WHERE P.pid = C.pid AND S.sid = C.sid

         AND S.sname = ‘AWS’ AND

        NOT EXISTS ( SELECT * FROM Catalog C1, Suppliers S1

                                WHERE P.pid = C1.pid AND S1.sid = C1.sid AND

                                S1.sname <> ‘AWS’ )

  • Find the sids of suppliers who charge more for some part than the average cost of that part (averaged over all the suppliers who supply that part).

           SELECT DISTINCT C.sid FROM Catalog C

           WHERE C.cost > ( SELECT AVG( C1.cost) FROM Catalog C1

           WHERE C1.pid = C.pid ).

  • For each part, find the sname of the supplier who charges the most for that part.

            SELECT P.pid, S.sname FROM Parts P, Suppliers S, Catalog C

            WHERE C.pid = P.pid AND S.sid = C.sid AND C.cost = ( SELECT MAX( C1.cost)

                                                                                                           FROM Catalog C1

                                                                                                           WHERE C1.pid = C.pid )

  • Find the sids of suppliers who supply only red parts.

             SELECT DISTINCT C.sid FROM Catalog C

            WHERE NOT EXISTS ( SELECT * FROM Parts P, Catalog C1

                                                  WHERE P.pid = C1.pid AND P.color = ‘Red’

                                                  AND C1.sid = C.sid )

  • Find the sids of suppliers who supply a red part and a green part.

           SELECT DISTINCT C.sid FROM Catalog C, Parts P

           WHERE C.pid = P.pid AND P.color = ‘Red’

           INTERSECT

           SELECT DISTINCT C1.sid FROM Catalog C1, Parts P1

          WHERE C1.pid = P1.pid AND P1.color = ‘Green’

  • Find the sids of suppliers who supply a red part or a green part.

            SELECT DISTINCT C.sid FROM Catalog C, Parts P

            WHERE C.pid = P.pid AND P.color = ‘Red’

            UNION

            SELECT DISTINCT C1.sid

            FROM Catalog C1, Parts P1

            WHERE C1.pid = P1.pid AND P1.color = ‘Green’

  • For every supplier that only supplies green parts, print the name of the supplier and the total number of parts that she supplies.

           SELECT S.sname, COUNT * AS PartCount FROM Suppliers S, Catalog C, Parts P

           WHERE P.pid = C.pid AND C.sid = S.sid

           GROUP BY S.sname, S.sid

           HAVING EVERY ( P.color = ‘Green’ )

  • For every supplier that supplies a green part and a red part, print the name and price of the most expensive part that she supplies.

           SELECT S.sname, MAX( C.cost ) AS MaxCost FROM Suppliers S, Parts P, Catalog C

           WHERE P.pid = C.pid AND C.sid = S.sid

           GROUP BY S.sname, S.sid

           HAVING ANY ( P.color = ‘Red’ ) AND ANY ( P.color = ‘Green’ )


Reference: Solution for Ramakrishnan and Gehrke by Niels Hvidberg Kjeldsen, September 2004.

Note: if anyone want to add anything in this please to comment in it.. Thanks for viewing the post.

One reply on “DBMS Query”

Comments are closed.