SQLite 3 reference · Aggregate Functions

SQLite SUM() Function

Add up the non-NULL values in a group. Returns NULL when there are no values; total() returns 0.0 instead.

SQLite 3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

sum(X)  |  sum(DISTINCT X)

Parameters

ParameterDescription
X A column or expression.

Return value

An integer if every value is an integer, otherwise a real number; NULL for no rows.

Examples

Total per orderTry it yourself
SELECT order_id, sum(quantity * unit_price) AS order_total
FROM order_items
GROUP BY order_id
ORDER BY order_total DESC
LIMIT 3;

Output

order_id | order_total
9 | 658
4 | 408.9
7 | 114.99
With a FILTER clauseTry it yourself
SELECT sum(stock) AS units, sum(price) FILTER (WHERE category_id = 1) AS book_prices
FROM products;

Output

units | book_prices
495 | 109.49

In other languages

Was this page helpful?