Question

SQL Queries (1)

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 bySuppliers. Write the following queries in SQL:
1) Find the pnames of parts supplied by Acme Widget Suppliers andno one else.2) Find the sids of suppliers who charge more for some partthan the average cost of that part (averaged over all the supplierswho supply that part).3) For each part, find the sname of the supplier who chargesthe most for that part.4) Find the sids of suppliers who supply only red parts.
0 1
Add a comment Improve this question Transcribed image text
Answer #1
(a) Find the names of suppliers who supply some yellow part.
RA: sname(Suppliers on (color=0yellow0(Parts) on Catalog))
SQL:
SELECT S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.color =’yellow’

(b) Find the sids of suppliers who supply some green part but not ared part.
RA: sid(Suppliers on (color=0green0(Parts) on Catalog))−
sid(Suppliers on (color=0red0(Parts) on Catalog))
SQL:
SELECT S.sid
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.color =’green’
EXCEPT
SELECT T.sid
FROM Suppliers T, Parts P, Catalog C
WHERE T.sid = C.sid AND P.pid = C.pid AND P.color =’red’

(c) Find the sids and snames of suppliers who supply a’bolt’ whose price is under 100
dollars or whose color is red.
There is ambiguity here too. Some of you may interpret the questionto mean a bolt
whose price is under 100 dollars or a bolt whose color is red. Someof you may interpret
it to mean a bolt whose price is under 100 dollars or a red part. Ihave given points to
both these versions.
RA: sid,sname(Suppliers on((pname=0bolt0^cost<100)_(color=0red0)(Parts onCatalog)))
RA: sid,sname(Suppliers on((pname=0bolt0^(cost<100_color=0red0))(Parts onCatalog)))
SQL:
SELECT S.sid, S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.pname =’bolt’ AND C.cost < 100
UNION
SELECT T.sid, T.sname
FROM Suppliers T, Parts P, Catalog C
WHERE T.sid = C.sid AND P.pid = C.pid AND P.color =’red’

SELECT S.sid, S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.pname =’bolt’ AND C.cost < 100
UNION
SELECT T.sid, T.sname
FROM Suppliers T, Parts P, Catalog C
WHERE T.sid = C.sid AND P.pid = C.pid AND P.pname =’bolt’ AND P.color = ’red’

(d) Find the sids of suppliers who supply all parts.
RA: sid((sid,pid(Catalog))/(pid(Parts))
SQL:
SELECT C.sid
FROM Catalog C
WHERE NOT EXISTS (( SELECT P.pid
FROM Parts P )
EXCEPT
(SELECT C1.pid
FROM Catalog C1
WHERE C1.sid = C.sid ))

(e) Find pids of parts supplied by at least two differentsuppliers
RA: (CatPairs(1 !sid1, 2 !pid1, 3 !cost1, 4 !sid2, 5 !pid2, 6!cost2), Catalog×
Catalog)
pid1((pid1=pid2)^(sid16=sid2)CatPairs)
SQL:
SELECT P.pid
FROM Catalog P, Catalog C
WHERE P.pid = C.pid AND P.sid <> C.sid

(f) Find the names of suppliers who supply some brown part.
RA: sname(Suppliers on (color=0brown0(Parts) on Catalog))
SQL:
SELECT S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.color =’brown’
(g) Find the sids of suppliers who supply some yellow part but nota red part.
RA: sid(Suppliers on (color=0yellow0(Parts) on Catalog))−
sid(Suppliers on (color=0red0(Parts) on Catalog))
SQL:
SELECT S.sid
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.color =’yellow’
EXCEPT
SELECT T.sid
FROM Suppliers T, Parts P, Catalog C
WHERE T.sid = C.sid AND P.pid = C.pid AND P.color =’red’


(h) Find the sids and snames of suppliers who supply a’nut’ whose price is under 10 dollars
or whose color is pink.
There is ambiguity here too. Some of you interpret the question asa nut whose price is
under 10 dollars or a nut whose color is pink. Some of youinterpret it as a nut whose
price is under 10 dollars and a part whose color is pink. Bothversions are treated as
correct answers.
RA: sid,sname(Suppliers on((pname=0nut0^cost<10)_(color=0pink0)(Parts onCatalog)))
RA: sid,sname(Suppliers on((pname=0nut0^(cost<10_color=0pink0))(Parts onCatalog)))
SQL:
SELECT S.sid, S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.pname =’nut’ AND C.cost < 10
UNION
SELECT T.sid, T.sname
FROM Suppliers T, Parts P, Catalog C
WHERE T.sid = C.sid AND P.pid = C.pid AND P.color =’pink’
SELECT S.sid, S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE S.sid = C.sid AND P.pid = C.pid AND P.pname =’nut’ AND C.cost < 10
UNION
SELECT T.sid, T.sname
FROM Suppliers T, Parts P, Catalog C
WHERE T.sid = C.sid AND P.pid = C.pid AND P.pname =’nut’ AND P.color = ’pink’
answered by: RB
Add a comment
Know the answer?
Add Answer to:
SQL Queries (1)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • EXERCISE 1 (SQL Queries) Consider the following schema: SUPPLIERS (SID : integer, SNAME : string, CITY...

    EXERCISE 1 (SQL Queries) Consider the following schema: SUPPLIERS (SID : integer, SNAME : string, CITY : string) PARTS (PID : integer, PNAME : string, COLOR : string) CATALOG (SID : integer, PID : integer, COST : real) The key fields are underlined, and the domain of each field is listed after the field name. Thus, SID is the key for SUPPLIERS, PID is the key for PARTS, and SID and PID together form the key for CATALOG. The CATALOG...

  • Consider the following schema: SUPPLIERS (SID: integer, SNAME: string, STREET: string, CITY: string, ZIP: string) PARTS...

    Consider the following schema: SUPPLIERS (SID: integer, SNAME: string, STREET: string, CITY: string, ZIP: string) PARTS (PID: integer, PNAME: string, COLOR: string) CATALOG (SID: integer, PID: integer, COST: real) The primary key attributes are underlined, and the domain of each attribute is listed after the attribute name. Thus, SID is the primary key for SUPPLIERS, PID is the primary key for PARTS, and SID and PID together form the primary key for CATALOG. Attribute SID in CATALOG is a foreign...

  • Please finish all parts, thanks! 2) Consider the following schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid:integer, pname: string, color: string ) Catalog( sid: integer, pid...

    Please finish all parts, thanks! 2) 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) Write the following queries in relational algebra. a) Find the names of suppliers who supply some red part b) Find the sids of suppliers who supply some red or green part c) Find the sids of suppliers who supply some red and some green part. d) Find the sids of suppliers...

  • Any help appreciated! Question 1 (5 points). Answer each of the following questions briefly. The questions...

    Any help appreciated! Question 1 (5 points). Answer each of the following questions briefly. The questions are based on the following relational schema: Emp( eid: integer, ename: string, age: integer, sala1l1: real) Works( eid: integer, did: integer, pet_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) 1. Give an example of a foreign key constraint that involves the Dept relation. 2. Write the SQL statements required to create the preceding relations, including appropriate versions of all primary and foreign...

  • Answer the following five (5) questions, based on the schema provided.

    C. Answer the following five (5) questions, based on the schema provided.Consider the following schema:Supplier (sid: integer, sname: string, address: string)Part(pid: integer, pname: string, , color: string)Catalog(sid: integer, pid: integer, cost: real)The relation Supplier stores suppliers and the primary key of that relation is sid. The relation Part stores parts, and pid is the primary key of that relation. Finally, Catalog stores which supplier supplies which part and at which cost (price). The primary key is the combination of the...

  • In .sql Given the following relational schemas, answer the following questions: Suppliers(sid: int, sname: VARCHAR(30), address:...

    In .sql Given the following relational schemas, answer the following questions: Suppliers(sid: int, sname: VARCHAR(30), address: VARCHAR(50)) Parts(pid: int, pname: VARCHAR(30), color: VARCHAR(10)) Catalog(sid: int, pid: int, cost: double) c. (8 points) List sid, sname, and address of all suppliers who supply at least one part. In other words, the answer must not show sid and sname of any supplier who does not have its sid in the Catalog table d. (4 points) Find all distinct black parts in the...

  • consider the following relational database that records details of parts and suppliers. Primary keys are underlined...

    consider the following relational database that records details of parts and suppliers. Primary keys are underlined and the Foreign Keys are identified with (FK). Suppliers (sid, sname, address, credit) Parts (pid, pname, color) Catalog (sid (FK), pid (FK), cost) The credit attribute denotes the size of the supplier’s credit account. The Catalog relation lists the prices charged for parts by Suppliers. Which of the following queries returns the snames of suppliers who supply every part? Select one or more: a....

  • Please HELP, THANK YOU SO MUCH Consider the below schema of the university database (keys are...

    Please HELP, THANK YOU SO MUCH Consider the below schema of the university database (keys are in bold and underline) Part(pid: string, pname: string, description: string, color: string) Department(did: string, dnamestring, address: string) Supplier(sid string,.sname:string, address:string) Order(did:string. sid string pidstring, time:string, quantityreal, pricereal) The key fields are underlined, and the domain of each field is listed after the field name. Thus pid is the key for Part, did is the key for Department, sid is the key for Supplier, and...

  • Given the following relational schema, write queries in SQL to answer the English questions. There is...

    Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid: integer, shipdate: Date/Time) ShippedProduct(sid:...

  • Consider the following database Relation Schemas: Relation Schemas: Suppliers(SID CHAR(5), Parts(pID VARCHAR(5), type VARCHAR(15), pName VARCHAR(35),...

    Consider the following database Relation Schemas: Relation Schemas: Suppliers(SID CHAR(5), Parts(pID VARCHAR(5), type VARCHAR(15), pName VARCHAR(35), sName VARCHAR(15), address VARCHAR(30, city VARCHAR(20), state CHAR(2), PRIMARY KEY(sID); PRIMARY KEY(pID) Catalog(sID CHAR(5), pID VARCHAR(5), ty SMALLINT, cost FLOAT (10,2), PRIMARY KEY(sid, pid), FOREIGN KEY(sid) REFERENCES Suppliers(SID), FOREIGN KEY (pid) REFERENCES Parts pID) The meaning of these relations is straightforward; for example, the Catalog relation lists the prices charged for parts by Suppliers. Instances of the relations Suppliers sName address SID cit state...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT