Why do we need GROUP BY with AGGREGATE FUNCTIONS?

后端 未结 4 1112
旧时难觅i
旧时难觅i 2020-11-30 09:51

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

4条回答
  •  死守一世寂寞
    2020-11-30 10:28

    If you wanted to add up all the numbers you would not have a GROUP BY:

    
    SELECT SUM(MonthlySalary) AS TotalSalary
    FROM Employee
    
    +-----------+
    |TotalSalary|
    +-----------+
    |777400     |
    +-----------+
    

    The point of the GROUP BY is that you get a separate total for each employee.

    +--------+------+
    |Employee|Salary|
    +--------+------+
    |John    |123400|
    +--------+------+
    |Frank   |413000|
    +--------+------+
    |Bill    |241000|
    +--------+------+
    

提交回复
热议问题