SQL query: find each item that costs more than the average and how much more -
i'm doing revision databases exam , 1 of questions follows:
given table items
(columns: itemid
, description
, unitcost
), formulate query find each item costs more average , how more average costs.
my answer far
select itemid, description, unitcost - avg(unitcost) items unitcost > (select avg(unitcost) items)
- would work? understand, expressions allowed in list of selected columns i'm not sure if extends use of functions
avg()
. - is there cleaner, clearer way express same thing? preferably using iso sql constructs because course rather theoretical , not connected specific dialect.
thanks friends :)
one query should work version of sql (that accepts explicit join syntax) be:
select i.itemid, i.description, i.unitcost - a.avg_cost cost_diff (select avg(unitcost) avg_cost items) join items on i.unitcost > a.avg_cost