totn MariaDB Functions

MariaDB: SUM Function

This MariaDB tutorial explains how to use the MariaDB SUM function with syntax and examples.

Description

The MariaDB SUM function returns the summed value of an expression.

Syntax

The syntax for the SUM function in MariaDB is:

SELECT SUM(aggregate_expression)
FROM tables
[WHERE conditions];

OR the syntax for the SUM function when grouping the results by one or more columns is:

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

Parameters or Arguments

expression1, expression2, ... expression_n
Expressions that are not encapsulated within the SUM function and must be included in the GROUP BY clause at the end of the SQL statement.
aggregate_expression
This is the column or expression that will be summed.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.

Applies To

The SUM function can be used in the following versions of MariaDB:

  • MariaDB 10

Example - With Single Expression

Let's look at some MariaDB SUM function examples and explore how to use the SUM function in MariaDB.

First, we'll go through an example of how to use the SUM function with a single expression in MariaDB.

For example:

SELECT SUM(file_size) AS "Total Size"
FROM pages
WHERE site_name = 'TechOnTheNet.com';

In this SUM function example, we will calculate the total of all file_size values where the site_name is 'TechOnTheNet.com'. We've aliased the SUM(file_size) expression as "Total Size". As a result, "Total Size" will display as the column title when the result set is returned.

Example - Using DISTINCT

Next, let's look at how to use the DISTINCT clause within the SUM function in MariaDB.

For example:

SELECT SUM(DISTINCT file_size) AS "Total Unique File Sizes"
FROM pages
WHERE site_name = 'TechOnTheNet.com';

In this SUM example, if there were two file_size values that were the same, only one of these values would be used in the SUM function calculation.

Example - Using Formula

The expression contained within the SUM function does not need to be a single field. You could also use a formula. Let's look at how to use a formula in the SUM function in MariaDB.

For example:

SELECT SUM(file_size * 1.75) AS "Total"
FROM pages
WHERE site_name = 'CheckYourMath.com';

In this SUM function example, the file_size column would be multiplied it by 1.75 and the result would be summed together.

Example - Using GROUP BY

Finally, let's look at how to use the GROUP BY clause with the SUM function in MariaDB.

If you are returning columns that are no encapsulated in the SUM function, you must use the GROUP BY clause.

For example:

SELECT site_id, SUM(file_size) AS "Total"
FROM pages
WHERE site_id < 100
GROUP BY site_id;

In this SUM function example, we must use a GROUP BY clause because the site_id field is not encapsulated in the SUM function. The site_id column must, herefore, be listed in the GROUP BY section at the end of the SQL statement.