WHERE
filters records before they are grouped; whereas HAVING
filters the results after they have been grouped. Expressions, using functions or operators, can be used in either clause (although aggregate functions like AVG()
cannot be used in the WHERE
clause as the records would not have been grouped when that clause is evaluated).
Thus your query filters a list of departments for those where the average salary of that department's male workers is greater than the overall (company) average salary.