SUM

The SQL SUM function is a built-in aggregate function in Structured Query Language (SQL) that is used to calculate the sum of a set of values in a specified column of a table. It is a powerful tool that allows you to easily compute the total of numeric data within a dataset. The SUM function returns the sum of all values from the select. Null values are ignored.

Syntax

The syntax for the SUM function is as follows:

SELECT SUM(column_name)
FROM table_name;

The SUM function works by adding up all the values in the specified column and returning the total. It can be used with any numeric data type, including integers, decimals, and floats.

Example

For example, suppose you have a table named Training_Course that contains the following data:

Training_Course

ID NAME DURATION PRICE
1 SQL 5 200
2 T-SQL 7 700
3 MySQL 5 600
4 PL/SQL 7 800
5 PostgreSQL 6 500

To calculate the total prices across all courses, you can use the SUM function as follows:

select SUM(PRICE) from Training_Course;

This query will return the result: 2800

In addition to the basic syntax, the SUM function can also be used in conjunction with other SQL functions, such as GROUP BY, HAVING, and WHERE, to calculate more complex aggregate values. It is a powerful tool that is essential for any data analyst or SQL developer working with large datasets.