SUM function in MariaDB

MariaDB SUM
To get the summed value of an expression, the MariaDB SUM function is used.

Syntax 1:

SELECT expressions, 
SUM (aggregate_expression)  
FROM table_name  
WHERE conditions;

Syntax 2: To calculate SUM and grouping the results by one or more columns.

SELECT expression1, expression2, ... expression_n,
SUM(aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n;

Parameters:
Aggregate_expression: It is used to specify the column or expression to be utilised by the aggregate function.

Example 1: Using Group By Clause.
Players Table:

ID	NAME	     SPORTS	INCOME
1	Sachin	     Cricket	5000000
2	Dhoni	     Cricket	8000000
3	Sunil	     Football	2000000
4	Srikanth     Badminton	1000000
5	Mary	     Boxing	3000000

Query:

SELECT sports, SUM(income) AS “Total Income”
FROM Players
GROUP BY sports;

Output:

SPORTS	        Total Income
Cricket	        13000000
Football	2000000
Badminton	1000000
Boxing	        3000000

Explanation:
The ‘Players’ is an already existing table. Here we are retrieving the sum of the income of the players of the same SPORTS group, using the Group By clause with an aggregate function SUM.

Example 2:
Players Table:

ID	NAME	       SPORTS	         INCOME
1	Sachin	       Cricket	         5000000
2	Dhoni	       Cricket	         8000000
3	Sunil	       Football	         2000000
4	Srikanth       Badminton         1000000
5	Mary	       Boxing	         3000000

Query:

SELECT SUM(income) FROM Players;

Output:

19000000

Explanation:
The total sum of the INCOME is the result.