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 Tree:
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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework