Concept Flow - SUM function
Start Query
Scan Table Rows
Extract Numeric Column Values
Add Values Together
Return Total Sum
End Query
The SUM function scans all rows, extracts the numeric values from a column, adds them up, and returns the total.
SELECT SUM(sales) FROM orders;
| Step | Action | Current Row sales | Running Total | Notes |
|---|---|---|---|---|
| 1 | Start query execution | - | 0 | Initialize sum to zero |
| 2 | Read first row | 100 | 100 | Add 100 to total |
| 3 | Read second row | 200 | 300 | Add 200 to total |
| 4 | Read third row | 150 | 450 | Add 150 to total |
| 5 | Read fourth row | 50 | 500 | Add 50 to total |
| 6 | No more rows | - | 500 | Return final sum |
| Variable | Start | After 1 | After 2 | After 3 | After 4 | Final |
|---|---|---|---|---|---|---|
| running_total | 0 | 100 | 300 | 450 | 500 | 500 |
SUM(column_name) adds all numeric values in the column. Ignores NULLs. Returns a single total value. Used in SELECT queries. Example: SELECT SUM(sales) FROM orders;