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
The query with the Sum function:
select sum(emp_sal_paid) as Total_paid_salaries from tbl_emp_salary_paid
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:
select emp_name,sum(emp_sal_paid)as total_paid from tbl_emp_salary_paid
group by emp_name
Also see: MySQL count