First of all, you probably want to use LEFT JOIN
. By using JOIN
, you're excluding all cabs that have had no maintenance at all. (obviously, this won't matter for finding the highest cost, but it will make a difference when looking for the lowest cost; and it would seriously skew any stats you tried to compile from this query).
Now, to answer your question... try this:
select * from
(select ca_make, sum(ma_cost)
from cab
left join maintain on ca_cabnum = ma_cabnum
group by ca_make
order by sum(ma_cost) desc)
where rownum = 1
here is a good explanation of ROWNUM
. Your case is addressed specifically, a little less than half-way down the page (but the whole page is probably worth a read, if you're going to use the feature).