The sum function of MySQL is used to find the sum of the various fields in the record.
To understand the SUM function consider the EMPLOYEE_TBL table has the following records:
Mysql> SELECT * from EMPLOYEE_TBL;
+------+------+------------+--------------------+
| id | name | work_date | daily_typing_pages
| +------+------+------------+--------------------+
| 1 | John | 2007-01-24 | The |
| 2 | Ram | 2007-05-27 | The |
| 3 | Jack | 2007-05-06 | 170 |
| 3 | Jack | 2007-04-06 | |
| 4 | Jill | 2007-04-06 | The |
| 5 | Zara | 2007-06-06 | A |
| 5 | Zara | 2007-02-06 | |
+------+------+------------+--------------------+
7 rows in Set (0.00 sec)
Now, assuming that you want to calculate the total number of all dialy_typing_pages based on the table above, you can use the following command:
Mysql> SELECT SUM (daily_typing_pages)
-> from Employee_tbl;
+-------------------------+
| SUM (daily_typing_pages) |
+-------------------------+
| 1610 |
+-------------------------+
1 row in Set (0.00 sec)
The sum of the various records that can be set by using the GROUP BY clause. The following example will summarize all the relevant records of a person, the total number of paper printed on each person.
mysql> SELECT name, SUM (daily_typing_pages)
-> from Employee_tbl GROUP by name;
+------+-------------------------+
| name | SUM (daily_typing_pages) |
+------+-------------------------+
| Jack | 270 |
| Jill | The |
| John | The |
| Ram | The |
| Zara | 650 |
+------+-------------------------+
5 rows in Set (0.17 sec)