Skip to main content
Returns the sum of non-null values for expr. You can use the DISTINCT keyword to compute the sum of distinct non-null values.

Syntax

Parameters

expr: the expression that evaluates to a numeric value. Supported data types are TINYINT, SMALLINT, INT, FLOAT, DOUBLE, or DECIMAL.

Return value

Data type mapping between input value and return value:
  • TINYINT -> BIGINT
  • SMALLINT -> BIGINT
  • INT -> BIGINT
  • FLOAT -> DOUBLE
  • DOUBLE -> DOUBLE
  • DECIMAL -> DECIMAL

Usage notes

  • This function ignores nulls.
  • An error is returned if expr does not exist.
  • If a VARCHAR expression is passed, this function implicitly casts the input into DOUBLE values. If the cast fails, an error is returned.

Examples

  1. Create a table named employees.
  2. Insert data into employees.
  3. Query data from employees.
    Text
  4. Use this function to compute sum. Example 1: Calculate the total sales of each region.
    Text
    Example 2: Calculate the total employee income of each region. This function ignores nulls and the income of employee id 777326 is not counted.
    Text
    Example 3: Calculate the total number of hobbies. The hobby column is of the STRING type and will be implicitly converted to DOUBLE during computation.
    Text
    Example 4: Use sum with the WHERE clause to calculate the total income of employees whose monthly income is higher than 30000.
    Text

keyword

SUM, sum