The SQLite SUM
function calculates and returns the sum of all values in a set, ignoring null values.
Syntax
Here is the syntax of SQLite SUM function:
SELECT SUM(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 |
SUM function example
SELECT SUM(PRICE) FROM books; SUM result: 120
The example above shows how to get the sum of prices from the books table. For this, the SELECT statement uses the PRICE column inside the brackets of the SUM function.
SELECT SUM(price) FROM books WHERE id=4; SUM result: NULL
The above example shows us that when the sum of a column with a null value is calculated, the SUM aggregate function will return NULL.