Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

group by employees.department_id

user480768Feb 13 2013 — edited Feb 13 2013
Hi,
I join my two tables (employees, departments) from famous HR schema and try to find the Average Salary by group by department_id for each department, using following SQL Statements, the Script Output give me ORA-00979: not a group by expression error message.

SELECT d.department_id AS "Department ID",
department_name,
TO_CHAR(AVG(salary), '$999,999.99') AS "Average Salary"
FROM employees e,
departments d
WHERE e.department_id = d.department_id
GROUP BY e.department_id;


However if I only use one table (employees) and group by department_id as showing below, the SQL statement can create the valid result.
SELECT e.department_id AS "Department ID",
TO_CHAR(AVG(salary), '$999,999.99') AS "Average Salary"
FROM employees e
GROUP BY e.department_id;


So, what is wrong with my first SQL statement? Please give me some help about how to solve the problem (join two tables with group by clause). Thanks for your help.

Kevin
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 13 2013
Added on Feb 13 2013
5 comments
593 views