SqlDatabaseBeginner

SUM()

Adds up numeric values across rows (optionally per group).

Review the syntaxStudy the examplesOpen the coding app
SELECT SUM(numeric_column) FROM table_name;

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Adds up numeric values across rows (optionally per group).

The SUM() aggregate function calculates the total sum of a numeric column's values within a result set. It operates by iterating through rows—either the entire table or groups defined by a GROUP BY clause—and accumulating non-NULL values. If a dataset contains only NULL values or is empty, SUM() returns NULL, which is a critical edge case compared to programming languages that might return 0. Performance is generally optimized through index scans (especially on covered indexes), but very large aggregations may require significant I/O and temporary disk space. The function supports the DISTINCT keyword to total only unique values, and it can be combined with arithmetic operators inside the parentheses to sum derived expressions (e.g., price * quantity). In high-concurrency environments, developers should be aware of transaction isolation levels as they can affect the consistency of the aggregate result.

Quick reference

Syntax

SELECT SUM(numeric_column) FROM table_name;

Inputs

Parameters

numeric_columnNumeric column to add up.

See it in practice

Examples

1

Calculating Total Revenue across all Orders

SELECT SUM(total_price) AS grand_total FROM orders WHERE status = 'completed';
Output:
[{"grand_total": 12450.50}]

A basic aggregation that sums the 'rice' column for all rows matching the status filter.

2

Summing Sales per Category using GROUP BY

SELECT category_id, SUM(amount) AS category_sum FROM sales GROUP BY category_id;
Output:
[{"category_id": 1, "category_sum": 5000}, {"category_id": 2, "category_sum": 3200}]

Groups the records by d and calculates the sum of the 'amount' column for each group.

3

Handling Potential NULL Results with COALESCE

SELECT COALESCE(SUM(bonus), 0) AS total_bonus_payout FROM employees WHERE department = 'Interns';
Output:
[{"total_bonus_payout": 0}]

Uses COALESCE to ensure that if the SUM result is NULL (due to no matching rows or all-null columns), it returns 0 instead.

Debug faster

Common Errors

1

DataException

Cause: Attempting to use SUM() on a column with a non-numeric data type, such as VARCHAR or DATE.

Fix: Ensure the column is a numeric type (INT, DECIMAL, FLOAT) or use CAST/CONVERT to transform the data before summing.

SELECT SUM(user_name) FROM users; -- Invalid for strings
2

LogicError

Cause: Returning NULL for empty datasets when the application logic expects a numeric 0, leading to errors in frontend calculations.

Fix: Wrap the SUM() function in a COALESCE or IFNULL function to provide a default value.

SELECT SUM(price) FROM products WHERE category = 'non_existent';

Runtime support

Compatibility

All SQL databasesSQL-92 (Standard)

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Adds up numeric values across rows (optionally per group).

olumn: Numeric column to add up.

DataException: Ensure the column is a numeric type (INT, DECIMAL, FLOAT) or use CAST/CONVERT to transform the data before summing. LogicError: Wrap the SUM() function in a COALESCE or IFNULL function to provide a default value.