SQL SUM Function

Quick summary: The SQL SUM() function returns the total of a numeric column.

SQL SUM Syntax

SELECT SUM(column_name) FROM table_name WHERE condition
SQL

SQL SUM Basic examples

SELECT SUM(total) FROM orders;
SQL
Output:
Total order amount

Adds all numeric values.

SQL SUM Real-world usage

SELECT SUM(total) FROM orders WHERE status = 'paid';
SQL
Output:
Total revenue

Calculates business metrics.

SQL SUM Edge cases

SELECT SUM(total) FROM orders WHERE status = 'canceled';
SQL
Output:
NULL

Returns NULL when no rows match.

SQL SUM Common mistakes

Assuming SUM returns 0

SUM returns NULL if no rows match.

Incorrect
SUM(total)
Correct
COALESCE(SUM(total), 0)

Use COALESCE to handle NULL.

SQL SUM Frequently Asked Questions

What does SUM() do in SQL?

Calculates the total of a numeric column.

Use case of SUM()?

Total calculations.

Handles NULL?

Ignores NULL values.

Return type?

Numeric.

Common mistake?

Using on non-numeric data.

Works with GROUP BY?

Yes.

Supports DISTINCT?

Yes.

Performance?

Efficient.

Used in reports?

Yes.

Alternative?

Manual aggregation.

Handles negative numbers?

Yes.

Best practice?

Validate numeric input.

SQL SUM Related SQL Keywords