I am trying to find the employees of company that have salary bigger than the average salary of all the employees. I would like to point out from the start that I don't want the average salary to be present in the final result, so I ommit it in the SELECT statement. These are the things I've tried:
SELECT employee.lastname,employee.firstname,employee.salary FROM employee
GROUP BY employee.salary
HAVING employee.salary > avg(employee.salary);
This results in an empty result table
However the following surprisingly returns all the employees of the company despite the '=' symbol.
SELECT employee.lastname,employee.firstname,employee.salary FROM employee
GROUP BY employee.salary
HAVING employee.salary = avg(employee.salary);
This returns empty table again:
SELECT employee.lastname,employee.firstname,employee.salary FROM employee
WHERE (SELECT avg(employee.salary) FROM employee
GROUP BY employee.salary
HAVING employee.salary > AVG(employee.salary));
So to conclude this post I would appreciate some insight about the right use of HAVING with an aggregate function, some insight about the reason that the snippets result in an empty table.