So i have two tables EMPLOYEE- Contains columns including EMPLOYEE_NAME, DEPARTMENT_ID and SALARY DEPARTMENTS - Contains columns including DEPARTMENT_NAME, and DEPARTMENT_ID
I need to display the department name and the average slary for each department and order it by the average salaries.
I am new to DBs and am having trouble. I try to do a subquery in the from field ( this subquery returns exactly what i need minus the department name which requires me to then join the departments table to the results) all the data in the subquery is in one table- employees. while department name is in the departments table.
here is what i tried.
SELECT D.DEPARTMENT_NAME, T.PERDEPT
FROM
(
SELECT DEPARTMENT_ID, AVG(SALARY) AS PERDEPT
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID
ORDER BY PERDEPT
) AS TEST T
JOIN DEPARTMENTS
ON D.DEPARTMENT_ID=T.DEPARTMENT_ID;
This returns a
SQL command not properly terminated
on the line with the AS TEST T
any and all help is greatly appreciated many thanks
DEPARTMENTSasD- Barbaros ÖzhanORDER BYin a subquery is nothing more than a wish that the optimizer will order your output a certain way. Put ordering on your top-level query, or get unstable output. Several answers are pointing out that you don't even need the subquery - for future askers, that's likely true given the (assumed) data model, but subquery aggregation is generally used to solve duplicate row issues. - Clockwork-Muse