I have a problem with one of my queries in SQLPLUS.
I have been asked to display the SuppCode, SuppName, and PostCode for all suppliers who satisfy the following contraints: Every stock item they supply has a price which is greater than the average of all stock items.
The three tables I’m working with are; Supppliers and Stocks and SupplyItems.
The Suppliers table looks looks like:
SUPPCODE SUPPNAME STREET TOWN COUNTRY POSTCODE TELNO FAXNO
With 8 seperate sample data (8 suppliers), and no null fields.
The Stocks table looks like:
ITEMNO STORECODE ITEMDESC QUANTITY UNITS REORDER PRICE SUPPCODE
With 40 seperate sample data (40 stock items), and no null fields.
The SupplyItems table looks like:
SUPPCODE CATNO STOCKNO PRICE
With 60 seperate sample data (60 supply items), and no null fields.
I have written the following query:
select distinct SU.SuppCode, Su.SuppName, PostCode
from Suppliers SU
LEFT JOIN SupplyItems SI ON SU.SuppCode = SI.SuppCode
where Price >
(select AVG(Quantity) from Stocks ST
where SI.StockNo = ST.StockNo);
I get the following output (4 values):
SUPPCODE SUPPNAME POSTCODE
S6 BSS LTD. B10 8SS
S2 ITX LTD. IT5 3TX
S3 FFG LTD. FY9 6FG
S4 OMM LTD. OM5 4MM
Now, I have been made aware that the output should contain either 2 or 3 records, so my query is wrong, though I can’t see why.
Any help would be appreciated.
This:
looks wrong – shouldn’t it rather be
?
EDIT: also,
probably doesn’t do what you want – this will return all suppliers who offer at least one item whose price is above the average; to get the suppliers that offer only items with price above the average, you’ll have to use a different approach, e.g. (untested)