WebDevelopersNotes logo

home-icon Home / MySQL / Free SQL tutorial – Finding the average and sum

Free SQL tutorial – Finding the average and sum

Free SQL tutorial - Finding the average and sum

Totalling column values with MySQL SUM

The SUM() aggregate function calculates the total of values in a column. You require to give the column name, which should be placed inside parenthesis.
Let’s see how much Bignet spends on salaries.

select SUM(salary) from employee_data;

+-------------+
| SUM(salary) |
+-------------+
|     1997000 |
+-------------+
1 row in set (0.00 sec)

SImilarly, we can display the total perks given to employees.

select SUM(perks) from employee_data;

+------------+
| SUM(perks) |
+------------+
|     390000 |
+------------+
1 row in set (0.00 sec)

How about finding the total of salaries and perks?

select sum(salary) + sum(perks) from employee_data;

+-------------------------+
| sum(salary)+ sum(perks) |
+-------------------------+
|                 2387000 |
+-------------------------+
1 row in set (0.01 sec)

This shows a hidden gem of the SELECT command. You can add, subtract, multiply or divide values. Actually, you can write full blown arithemetic expressions. Cool!

MySQL AVG() – Finding the Average

The AVG() aggregate function is employed for calculating averages of data in columns.

select avg(age) from employee_data;
+----------+
| avg(age) |
+----------+
|  31.6190 |
+----------+
1 row in set (0.00 sec)

This displays the average age of employees in Bignet and the following displays the average salary.

select avg(salary) from employee_data;
+-------------+
| avg(salary) |
+-------------+
|  95095.2381 |
+-------------+
1 row in set (0.00 sec)

Sponsored Links

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

Sponsored Links

Tips

How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]

Fact

Steve Jobs struck a deal with the venture capital division of Xerox by letting them invest $1 million in exchange for a look at the technologies and devices the company was working on at PARC. Jobs successfully imported the mouse, GUI and other technologies into Apple machines. [more...]

We use cookies to give you the best possible website experience. By using WebDevelopersNotes.com, you agree to our Privacy Policy