MySQL sum function with select statement

The sum function of MySQL

A few main points about the Sum function of MySQL:

  • The sum function calculates the values of the given numeric column and returns the sum.
  • Generally, the MySQL Sum function is used with Select queries along with the where clause.
  • This is also used in the group by clause.

The sum function syntax

This is how you can use the Sum function:

Select sum(numeric_column_name) from table_name

Where col_name = value

The Sum examples in select and group by

We will use one of our tables, tbl_emp_salary_paid created to store salaries of employees along with their names.

Sum function with select example

In the following example, the emp_sal_paid is used in the Sum function to return the total of salaries paid.

The query with the Sum function:

The Sum with Group by example

Following is an example that uses the group by clause with the sum function. In that query, we will return the total salary paid to each employee. You can see query and output by clicking the link below:

The MySQL Sum query with group by clause:

Also see: MySQL count

Was this article helpful?

Related Articles

Leave A Comment?