PostgreSQL Aggregate Functions and Group By: Get the department ID and the total salary payable in each department
10. Write a query to get the department ID and the total salary payable in each department.
Sample Solution:
Code:
SELECT department_id, SUM(salary)
FROM employees
GROUP BY department_id;
Sample table: employees
Output:
pg_exercises=# SELECT department_id, SUM(salary) pg_exercises-# FROM employees pg_exercises-# GROUP BY department_id; department_id | sum ---------------+----------- 90 | 58090.00 20 | 19060.00 100 | 51780.00 40 | 6530.00 110 | 20360.00 80 | 295990.00 70 | 10030.00 50 | 157750.00 60 | 28950.00 30 | 25080.00 10 | 4430.00 0 | 7030.00 (12 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 find the manager ID and the salary of the lowest-paid employee under that manager.
Next: Write a query to get the average salary for each post excluding programmer.
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