Page Stats
Visitor: 376
SQL Aggregate functions perform calculation on a set of values and return a single value. The following table list some useful aggregate functions:
Function | Description |
---|---|
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