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 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 Expression: Get the department ID and the total salary payable in each department.

Relational Algebra Tree:

Relational Algebra Tree: Get the department ID and the total salary payable in each department.

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?