MySQL Tutorial

Define MySQL

MySQL Create Database

MySQL Create Table

MySQL Constraints

MySQL Case Sensitivity

MySQL Insert

MySQL Select

MySQL Data Types

MySQL OrderBy

MySQL Top

MySQL Distinct

MySQL Update

MySQL Delete

MySQL Truncate

MySQL Drop

MySQL Alter

MySQL Join

MySQL Aggregate Function

MySQL String Function

MySQL Date Function

Page Stats

Visitor: 268

MySQL Aggregate Functions

SQL Aggregate functions perform calculation on a set of values and return a single value. The following table list some useful aggregate functions:

FunctionDescription
SUM()Returns the sum of values
AVG()Returns the average of values
COUNT()Returns the number of rows in a result set
MAX()Returns the maximum value
MIN()Returns the minimum value

Example 1: MySQL Sum Function

select sum(salary) from emp
select sum(salary) as 'Total salary' from emp

Example 2: MySQL Avg Function

select avg(salary) as 'Total salary' from emp

Example 3: MySQL COUNT Function

SELECT count(salary) FROM `emp`
//count only values except NULL value.

Example 4: MySQL MAX Function

select max(salary) from emp
select * from emp WHERE salary = max(salary) //error
select * from emp WHERE salary = (SELECT max(salary) from emp)
SELECT * FROM `student` WHERE salary > ( SELECT avg(salary) from student)

Example 5: MySQL MIN Function

select min(salary) from student