sql-server
  1. sql-server-sum-function

SUM Function - (SQL Server Aggregate Function)

The SUM function is an SQL Server Aggregate Function used to calculate the sum of column values in a selected table.

Syntax

SELECT SUM(column_name) FROM table_name

Here, SUM calculates the sum of values in column_name for every row in table_name.

Example

Consider a table named Sales:

Customer Order_Date Amount
A 2022-01-01 1000
B 2022-01-02 2000
C 2022-01-03 3000
D 2022-01-04 4000
E 2022-01-05 5000

To find the total amount of sales, the following SQL query can be used:

SELECT SUM(Amount) FROM Sales

The SUM function will calculate the sum of all values in the Amount column of the Sales table and return the total as the output.

Output

The output of the above SQL query will be:

15000

Explanation

The SUM function calculates the total sum of the values in the Amount column of the Sales table and returns the result.

Use

The SUM function is useful in scenarios where we need to calculate the total or sum of a column value in a table, such as in accounting or financial-related applications.

Important Points

  • The SUM function is used to calculate the total sum of a numeric column in a selected table.
  • The SUM function works only with numerical data types.
  • The SUM function returns null if the selected column has no values.

Summary

In this page, we discussed the SUM function, which is an SQL Server Aggregate Function used to calculate the sum of column values in a table. We also covered its syntax, example, output, explanation, use, and important points. The SUM function is an essential tool in SQL Server used to calculate the total value of the numerical data in a selected column.

Published on: