Module 5 SQLFunctions
Module 5 SQLFunctions
Learning Outcomes
Indicative Content:
Count
SUM
AVG
Module Title: MYSQL FUNCTIONS
WHAT IS FUNCTION? :
A function is a block of organized, reusable code that is used to perform a
single, related action.
Functions provide better modularity for your application and a high degree of
code reusing.
A function is a stored program that you can pass parameters into and then
return a value.
Count (*)
Count (expression)
Count (distinct)
COUNT(*) Function: This function uses the SELECT statement to returns the count of
rows in a result set.
Syntax
The following are the syntax of the COUNT() function:
1. SELECT COUNT (aggregate_expression)
2. FROM table_name
3. [WHERE conditions];
COUNT(expression)
Execute the following query that uses the COUNT(expression) function to calculates
the total number of employees name available in the table:
Try:
Output
Example2
Execute the following statement that returns all rows from the employee table
and WHERE clause specifies the rows whose value in the column emp_age is
greater than 32:
Output
Example3
This statement uses the COUNT(distinct expression) function that counts the Non-
Null and distinct rows in the column emp_age:
SELECT COUNT(DISTINCT emp_age) FROM employees;
The MySQL sum() function is used to return the total summed value of an
expression
Syntax
Following are the syntax of sum() function in MySQL:
1. SELECT SUM(aggregate_expression)
2. FROM tables
3. [WHERE conditions];
MYSQL AVG() FUNCTION
The MySQL avg() is an aggregate function used to return the average value of an
expression in various records.
Syntax
The following are the basic syntax an avg() function in MySQL:
SELECT AVG(aggregate_expression)
FROM tables
[WHERE conditions];
Practical Example:
SELECT COUNT(income) as "RecordCount",
SUM(income) as SUM,
AVG(income) AS AVERAGE
FROM employees;
Output: