The sum function in MySQL calculates the sum of a set of values present in a column.
MySQL Sum Function Syntax
Let us see a real-time query on the SUM function.
Consider a table employee storing the data of the employees of an organization. One of the fields in the table is the address that holds the employees' residential address. One of the peculiar observations is that all employees reside in "Fondren, Houston, TX." One of the addresses is '450 Fondren, Houston, TX'. 450 is the house number of the employee. Write a query to determine the sum of all house numbers resided in by the employees.
Here is the table used for this.
select sum( address like '450%') from emp;
- So the output gives us the sum of addresses starting with 450 from the emp table.
This is one way of using the sum function in MySQL and can be used in many ways.