MySQL Addition

MySQL Addition returns you the sum value of a specific column for a group in a table.

MySQL Addition

MySQL Addition

     

MySQL Addition returns you the sum value of a specific column for a group in a table. The Aggregate function sum() is used to perform the addition in SQL Query. We can make use of Aggregate function with group by and order by clause.

Syntax:

SUM([DISTINCT] column_name):

Returns the sum of the values of the column column_name. If there are no rows then SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of the column.

Understand with Example

The Tutorial illustrates an example of 'MySQL Addition'. To understand the example we have a table employee in the database. The select order by query is used to sort the records by specific column. In this example we sort the records of name and salary on the basis of salary column from table 'employee'.

Query

 

select name, salary from employee order by salary;

 

Output

 

+--------+--------+
| name   | salary |
+--------+--------+
| Kamal  | 101    |
| Ajay   | 105    |
| Nitin  | 110    |
| Rajiv  | 120    |
| Vinay  | 130    |
| Sachin | 150    |
| Karan  | 160    |
+--------+--------+

In this example, the sum() function is used to calculate sum of salary of all employees in a table 'employee'.

Query

 

 select sum(salary) from employee;

 

Output

 

+-------------+
| sum(salary) |
+-------------+
| 876         |
+-------------+