w3resource

PostgreSQL Aggregate Functions and Group By: Get the average salary for all departments working more than 10 employees


14. Write a query to get the average salary for all departments working more than 10 employees.

Sample Solution:

Code:

SELECT department_id, AVG(salary), COUNT(*) 
FROM employees 
GROUP BY department_id 
HAVING COUNT(*) > 10;

Sample table: employees


Output:

pg_exercises=# SELECT department_id, AVG(salary), COUNT(*)
pg_exercises-# FROM employees
pg_exercises-# GROUP BY department_id
pg_exercises-# HAVING COUNT(*) > 10;
 department_id |          avg          | count
---------------+-----------------------+-------
            80 | 8969.3939393939393939 |    33
            50 | 3505.5555555555555556 |    45
(2 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