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)

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