I saw an example where there was a list (table) of employees with their respective monthly salaries. I did a sum of the salaries and saw the exact same table in the ouptput. That was strange.
Here is what has to be done - we have to find out how much money we pay this month as employee salaries. For that, we need to sum their salary amounts in the database as shown:
SELECT EmployeeID, SUM (MonthlySalary)
FROM Employee
GROUP BY EmpID
I know that I get an error if I don't use GROUP BY
in the above code. This is what I don't understand.
We are selecting EmployeeID from the Employee table. SUM()
is being told that it has to add the MonthlySalary column, from the Employee table. So, it should directly go and add those numbers up instead of grouping them and then adding them.
Thats how a person would do it - look at the employee table and add all the numbers. Why would they take the trouble to group them and then add them up?