Hit enter after type your search item

What is SUM function in SQL?

The SQL SUM is an aggregate function that is used to return the sum of a numeric column for all the values or DISTINCT values.

If null values are found in the given numeric column, these are ignored by SUM function.

The SUM function example with Select

For the example, have a look at the table in the graphic below. The tbl_emp_salary_paid stores the salaries paid to each employee every month (for the demo only).

By using the SUM function, we will get the total amount paid to all employees without using where clause.



The example of using COUNT and SUM functions together

What if you want to know the count of rows for what the sum is calculated. For example, in our employee salaries table, we want to know the number of times salary paid along with sum. The query and the result are as follows from the demo table:

SQL SUM count

Using SUM with DISTINCT clause

As mentioned in the introductory part, you may get the sum of distinct values by using the SUM function. The following query shows how to use the DISTINCT clause with SUM SQL function for that:

SQL SUM Distinct

The example of using Group by clause with SUM function

What about getting the sum of salaries paid to each employee separately rather than the accumulative sum of the complete column?

The answer for this is using the Group by clause with SUM function. See the following query and resultant table graphic:

SQL SUM group by
This div height required for enabling the sticky sidebar