Хотя приведенные ниже запросы показывают тот же результат, что и другие ответы, приятно показать пользователям, как это можно сделать в качестве альтернативы:
--Method 1 (Davek's select of 1st row over Order by) Brilliant!
--Method 2 (Thomas' where = sub-query result)
--Method 3 (Thomas' based on ranking)
--Method 4 (Inner join sub-queries)
select distinct a.department, a.wage from
(select distinct department, AVG(wage) as wage from employees group by department) as a
inner join
(select Max(wage) as wage from
(select distinct department, AVG(wage) as wage from employees group by department) as x) as b
on a.wage = b.wage
where a.wage = b.wage
--Method 5 (AVG wage in (sub-query))
select distinct a.department, a.wage
from (select distinct department, AVG(wage) as wage from employees group by department) as a
Where a.wage in
(select Max(wage) as wage from
(select distinct department, AVG(wage) as wage from employees group by department) as x)
Ожидая увидеть пользовательскую функцию для этого выбора также:)