w3resource

PostgreSQL Aggregate Functions and Group By: Get the average salary for each post excluding programmer


11. Write a query to get the average salary for each post excluding programmer.

Sample Solution:

Code:

SELECT job_id, AVG(salary) 
FROM employees 
WHERE job_id <> 'IT_PROG' 
GROUP BY job_id;

Sample table: employees


Output:

pg_exercises=# SELECT job_id, AVG(salary)
pg_exercises-# FROM employees
pg_exercises-# WHERE job_id <> 'IT_PROG'
pg_exercises-# GROUP BY job_id;

   job_id   |          avg
------------+------------------------
 Salesman   |  7310.0000000000000000
 AC_ACCOUNT |  8330.0000000000000000
 SA_MAN     |     12230.000000000000
 AD_PRES    |     24030.000000000000
 AC_MGR     | 12030.0000000000000000
 FI_MGR     | 12030.0000000000000000
 AD_ASST    |  4430.0000000000000000
 MK_MAN     | 13030.0000000000000000
 PU_CLERK   |  2810.0000000000000000
 HR_REP     |  6530.0000000000000000
 PR_REP     | 10030.0000000000000000
 FI_ACCOUNT |  7950.0000000000000000
 SH_CLERK   |  3245.0000000000000000
 AD_VP      |     17030.000000000000
 SA_REP     |  8340.3448275862068966
 ST_CLERK   |  2815.0000000000000000
 MK_REP     |  6030.0000000000000000
 PU_MAN     | 11030.0000000000000000
(18 rows)

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming