MySQL SUM
In MySQL, the SUM function is used to get the summed value of an expression.
Syntax:
SELECT SUM (aggregate_expression) FROM table_name WHERE conditions;
Parameters:
Aggregate_expression: It is used to specify the column or expression to be utilized by the SUM function.
Example: Items table:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | 100 |
4 | Electronics | 90 |
5 | Sports | 50 |
Query:
SELECT name, SUM(quantity) AS “Quantity” FROM items GROUP BY name;
Output:
NAME | QUANTITY |
Electronics | 120 |
Sports | 95 |
Fashion | 100 |
Explanation:
The ‘items’ is an already existing table from which we are calculating the sum of quantities for each unique item name.