MySQL Aggregate Function Exercises: Get the department ID and the total salary payable in each department
MySQL Aggregate Function: Exercise-10 with Solution
Write a query to get the department ID and the total salary payable in each department.
Sample table: employees
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | ......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Code:
-- Calculating the total salary for each department
SELECT department_id, SUM(salary)
-- Selecting data from the employees table
FROM employees
-- Grouping the result set by department_id
GROUP BY department_id;
Explanation:
- This SQL query calculates the total salary for each department in the organization.
- The SELECT statement retrieves the department_id column and calculates the sum of salaries (SUM(salary)) for each department.
- The FROM clause specifies the employees table from which the data is being selected.
- The GROUP BY clause groups the result set by the department_id column, allowing for the aggregation of salaries for each department.
- This query is useful when you want to analyze the total salary expenditure for each department, helping to understand the distribution of resources.
Relational Algebra Expression:
Relational Algebra Tree:
Pictorial Presentation of the above query
Go to:
PREV :Write a query to find the manager ID and the salary of the lowest-paid employee for that manager.
NEXT :Write a query to get the average salary for each job ID excluding programmer.
MySQL Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
