Finding the average and 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!
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)
|
« Previous
|
Next »
|
Finding the minimum and maximum values MySQL provides inbuilt functions to find the minimum and maximum values. SQL provides 5 aggregate functions. They are: MIN(): ...
Naming Columns MySQL allows you to name the displayed columns. So instead of f_name or l_name etc. you can use more descriptive terms. This is ...
Possible Answers create database addressbook; OR CREATE DATABASE addressbook; Note: SQL statements are case-insensitive, though table names and database names might be sensitive to case ...
Creating tables In this section of the mysql training course we will explore the MySQL commands to create database tables and selecting the database. Databases ...
A little more on the MySQL SELECT statement The MySQL SELECT command is something like a print or write command of other languages. You can ...