Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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 | 8955.8823529411764706 |    34
            50 | 3475.5555555555555556 |    45
(2 rows)

Relational Algebra Expression:

Relational Algebra Expression: Get the average salary for all departments working more than 10 employees.

Relational Algebra Tree:

Relational Algebra Tree: Get the average salary for all departments working more than 10 employees.

Practice Online


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

Previous: Write a query to get the job ID and maximum salary of each post for maximum salary is at or above $4000.
Next: PostgreSQL String() Function - Exercises, Practice, Solution

What is the difficulty level of this exercise?