0% found this document useful (0 votes)
42 views

Free SQL Tutorial Page 19

The document discusses using SQL aggregate functions like SUM and AVG to calculate totals and averages from columns in a database table. It provides examples of finding the total salary and perks for employees, calculating a sum and average, and using arithmetic expressions in SELECT statements. The questions at the end ask about using these functions to calculate things like the sum of ages, total years of service, and the percentage that perks are of total salary.

Uploaded by

harryforu_96
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Free SQL Tutorial Page 19

The document discusses using SQL aggregate functions like SUM and AVG to calculate totals and averages from columns in a database table. It provides examples of finding the total salary and perks for employees, calculating a sum and average, and using arithmetic expressions in SELECT statements. The questions at the end ask about using these functions to calculate things like the sum of ages, total years of service, and the percentage that perks are of total salary.

Uploaded by

harryforu_96
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

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)

1. Display the sum of ages of employees working at Bignet.


2. How would you calculate the total of years of service the employees of Bignet have in the
company?

3. Calculate the sum of salaries and the average age of employees who hold "Programmer"
title.
4. What do you understand from the following statement?

5. select (SUM(perks)/SUM(salary) * 100)

6. from employee_data;

You might also like