The SUM() function in SQL is used to calculate the sum of numeric columns. It can calculate sums based on specified columns, filters, aliases, grouping and aggregation of multiple columns, but only handles numeric values and ignores NULL values.
Usage of SUM() function in SQL
The SUM() function in SQL is used to calculate a set of The sum of the numbers. It is an aggregate function that operates on a set of rows or tables.
Syntax:
<code class="sql">SUM(expression)</code>
Where:
Usage:
<code class="sql">SELECT SUM(salary) FROM employees;</code>
<code class="sql">SELECT SUM(salary) FROM employees WHERE department = 'Sales';</code>
<code class="sql">SELECT SUM(salary) AS total_salary FROM employees;</code>
<code class="sql">SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department;</code>
<code class="sql">SELECT SUM(salary) + SUM(bonus) AS total_compensation FROM employees;</code>
Note:
The above is the detailed content of How to use sum function in sql. For more information, please follow other related articles on the PHP Chinese website!