I am getting familiar with pl\sql and I have a question about certain task.
I want to find the job with the lowest average salary, this is the solution from the script:
SELECT job_id, AVG(salary)
FROM employees
GROUP BY job_id
HAVING AVG(salary) = (SELECT MIN(AVG(salary))
FROM employees
GROUP BY job_id);
and the answer is job_id -> PU_CLERK, AVG(salary) -> 2700
My first question is why this code won't work:
SELECT job_id, MIN(AVG(salary))
FROM employees
GROUP BY job_id
I get an "not a single-group group function" error. By googeling the error, almost every solution is: To resolve the error, you can either remove the group function or column expression from the SELECT clause or you can add a GROUP BY clause that includes the column expressions.
Then why isn't this working? I have column expression in group by clause. Someone already posted about this but everyone just gave different solution. I would really appreciate if someone can actually explain.
With this query:
SELECT job_id, AVG(salary)
FROM employees
GROUP BY job_id
ORDER BY(salary)
I get a list of average salaries and PU_CLERK is the lowest with 2700, just like in the solution. But, if a limit a number of rows displayed with ROWNUM, i get different solution. So, this query:
SELECT job_id, AVG(salary)
FROM employees
WHERE ROWNUM = 1
GROUP BY job_id
ORDER BY(salary)
gives solution of SH_CLERK and 2600. Why is that? I tried working with FETCH but that just didn't work.
Also, this query:
SELECT MIN(AVG(salary))
FROM employees
GROUP BY job_id
gives solution of 2700, but it is missing a job_id so I can know which job position that actually is.
Thanks in advance to everyone for the answers and comments!