SUM
Adds numeric values, commonly for totals and amounts.
Overview
SUM adds numeric values across all selected rows or within each GROUP BY group. It ignores NULL, while a group containing only NULL values returns NULL. For monetary data, choose an exact numeric type with suitable precision.
Basic syntax
SELECT SUM(amount) AS total_amount FROM orders;
Examples
SUM example
Sum the amount column in orders and verify it equals the four sample amounts combined.
Sample table
Table:
orders| amount |
|---|
| 1200 |
| 900 |
| 1500 |
| 700 |
Run result
| total_amount |
|---|
| 4300 |
Common issues
Does SUM ignore NULL?
Yes. SUM normally ignores NULL; if every input is NULL, the result may also be NULL.
Can SUM be used with GROUP BY?
Yes. It commonly totals amounts or quantities by customer, department, or date.
Should SUM be used to count rows?
COUNT(*) is the usual row-count function; SUM is for numeric columns or conditional expressions.
Tags
Was this explanation clear?
Thanks. This feedback will inform the next content review.