Consider the following schema: SUPPLIERS (SID : integer, SNAME : string, ADDRESS
ID: 3805301 • Letter: C
Question
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 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 relation lists the prices charged for parts by suppliers. CATALOG.SID is a foreign key referring to SUPPLIERS.SID and CATALOG.PID is a foreign key referring to PARTS.PID.
Write the following queries in SQL:
1. For every supplier that supplies a green part and a red part, print her SID and the PID, the name and the price of the most expensive parts that she supplies.
2. List the PID, PNAME and average cost of all parts.
3. Find the average cost of parts supplied by suppliers named 'Yosemite Sham'.
Explanation / Answer
1> Not sure what this statement means - "the price of the most expensive parts that she supplies."
- If question demands most expensive parts(plural) The code will be.
select SID,C.PID,P.PNAME,COST
From CATALOG C
Join PARTS P on C.PID = P.PID
where (P.COLOR='RED' or P.COLOR='green')
- If question demands most expensive part(Singular)
select SID,C.PID,P.PNAME,MAX(COST)
From CATALOG C
Join PARTS P on C.PID = P.PID
where (P.COLOR='RED' or P.COLOR='green')
2>
- If your SQL supports avg() function
select PID,PNAME, (select avg(COST) from PARTS) as AVERAGE_COST
from PARTS;
- If your SQL does not support AVG() function
select PID,PNAME, (select sum(COST)/count(COST) from PARTS) as AVERAGE_COST
from PARTS;
3>
Select avg(COST)
from CATALOG C
join PARTS P on P.PID = C.PID
join SUPPLIERS S on S.SID = C.SID
where S.SNAME = 'Yosemite Sham'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.