Sum Function in MySQL

Profile picture for user arilio666

The sum function in MySQL calculates the sum of a set of values present in a column.

MySQL Sum Function Syntax

SUM(expression)

Example

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.

mysql sum function

select sum( address like '450%') from emp;

mysql sum function example

  • So the output gives us the sum of addresses starting with 450 from the emp table.

Conclusion:

This is one way of using the sum function in MySQL and can be used in many ways.

Tags