group by employees.department_id
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