The SQLite TOTAL
function calculates and returns the sum of all values in a set, ignoring null values.
The SQLite TOTAL function is identical to the SUM
function, both are aggregate functions
and both return the sum of all values in a set of values. The only difference is when there are no values in a column, SUM returns NULL, and TOTAL returns 0.
Syntax
Here is the syntax of SQLite TOTAL function:
SELECT TOTAL(column_name) FROM table_name;
Example
Table of books
ID | NAME | PRICE |
---|---|---|
1 | SQLite | 10 |
2 | SQL | 20 |
3 | PL/SQL | 30 |
4 | PHP | NULL |
5 | Python | 20 |
6 | HTML | 40 |
TOTAL function example
SELECT TOTAL(PRICE) FROM books; TOTAL result: 120
The example above shows how to get the total sum of prices from the books table. For this, the SELECT statement uses the PRICE column inside the brackets of the TOTAL function.
SELECT TOTAL(price) FROM books WHERE id=4; TOTAL result: 0
The above example shows us that when the total sum of a column with a null value is calculated, the TOTAL aggregate function will return 0.