The SUM function in SQL calculates the sum of non-null values in the specified column. The syntax is SUM(expression), where expression is the column or expression to be calculated. The SUM function can be used to calculate sums, summarize data, and is suitable for financial reporting, inventory management, and data analysis.
Usage of SUM function in SQL
Overview:
Usage of SUM function Calculates the sum of all non-null values in the specified column.
Syntax:
<code class="sql">SUM(expression)</code>
Where:
expression
is the column or expression to be calculated. Purpose:
Detailed explanation:
The SUM function accepts an expression as a parameter, which can be a column reference or a mathematical operation. It processes the data set row by row, ignoring null values, and accumulating each non-null value.
Example:
<code class="sql">SELECT SUM(sales) FROM orders;</code>
This query will calculate the sum of all non-null values in the sales
column in table orders
.
GROUPING SUM:
The SUM function can also be used with the GROUP BY clause to summarize values within a specified group. For example:
<code class="sql">SELECT department, SUM(salary) FROM employees GROUP BY department;</code>
This query will calculate the total employee salary for each department.
Note:
NULL
. The above is the detailed content of Usage of sum function in sql. For more information, please follow other related articles on the PHP Chinese website!