Pages

Avg and group by

Now we are interested in the average salary for the departments we have seen in the previous post.

We apply the ROUND() function to the result of AVG() to keep the result more readable. The rest of the statement is quite the same as the previous example.

To add a diversion, we show the result in ascending order, and we put the ASC modified esplicitely in the ORDER BY clause, even though that is not necessary, since ASC is the default:

select department_id, round(avg(salary))
from employees
group by department_id
order by avg(salary) asc;

If you want a fun introduction to SQL, consider reading Head First SQL.

No comments:

Post a Comment