21.2. SUM() Functions

This function works only with numeric values. You should also consult your particular product’s manuals to find out the precision of the results for exact and approximate numeric data types.

SUM([ALL] <value expression>) returns the numeric total of all known values. The NULLs are removed before the summation takes place. An empty set returns an empty result set, not a zero. If there are other columns in the SELECT list, then that empty set will be converted into a NULL.

SUM(DISTINCT <value expression>) returns the numeric total of all known, unique values. The NULLs and all redundant duplicates were removed before the summation took place. Again, an empty set returns an empty result set, not a zero.

That last rule is hard ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.