I want to group MIN()
and AVG()
function in SQL, that is,
MIN(AVG(column_name))
Is there a way to do the grouping?
I tried this:
SELECT
`instructer`.`dept_name`, AVG(`instructer`.`salary`)
AS
MinAvgSalary
FROM
`instructer`
GROUP BY
`instructer`.`dept_name`
HAVING
AVG(`instructer`.`salary`) = (SELECT MIN(AVG(`instructer`.`salary`)) FROM `instructer` GROUP BY `instructer`.`dept_name`)
But MySQL said:
1111 - Invalid use of group function
This is my instructor table:
You can use a subselect
select t.dept_name, min(t.AvgSalary) AS MinAvgSalary
from (
SELECT
`instructer`.`dept_name` AS dept_name
, AVG(`instructer`.`salary`) AS AvgSalary
FROM `instructer`
GROUP BY `instructer`.`dept_name` ) t
GROUP BY t.dept_name
HAVING t.AvgSalary = min(t.AvgSalary)
Try to use LIMIT 1
in the last query:
SELECT
`instructer`.`dept_name`, AVG(`instructer`.`salary`)
AS
MinAvgSalary
FROM
`instructer`
GROUP BY
`instructer`.`dept_name`
HAVING
AVG(`instructer`.`salary`) =
(SELECT AVG(`instructer`.`salary`) as AvgSalary
FROM `instructer`
GROUP BY `instructer`.`dept_name`
ORDER BY AvgSalary LIMIT 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With