Tuesday, April 2, 2019

SQL Subquery Exercises: Display the department id and the total salary for those departments which contains at least one employee

 


SQL SUBQUERY: Exercise-22 with Solution

Write a query to display the department id and the total salary for those departments which contains at least one employee.

Sample table: employees


Sample table: departments


Sample Solution:

SELECT departments.department_id, result1.total_amt 
FROM departments,  
( SELECT employees.department_id, SUM(employees.salary) total_amt  
FROM employees  
GROUP BY department_id) result1 
WHERE result1.department_id = departments.department_id;

Sample Output:

department_id	total_amt
10		4400.00
20		19000.00
30		24900.00
40		6500.00
50		156400.00
60		28800.00
70		10000.00
80		304500.00
90		58000.00
100		51600.00
110		20300.00

N.B. : In certain instances not null is removed in table structure, so results may vary.

Pictorial Presentation:

SQL Subqueries: Display the department id and the total salary for those departments which contains at least one employee.

Practice Online


 

No comments:

Post a Comment

Reference

https://notesformsc.org/locks-database-management https://mycareerwise.com/content/concept-of-lossless-and-lossy-join-decomposition/content/...