3. 03/15/2025 08:51 PM 3
الصناعة تكنولوجيا كلية
والطاقة
MySQL Functions
• Basically, it is a set of SQL statements that accept only input parameters, perform actions
and return the result. A function can return only a single value or a table
• Types of Function
– A scalar function is a function that operates on scalar values -- that is, it takes one (or
more) input values as arguments directly and returns a value.
– An aggregate function is a function that operates on aggregate data -- that is, it takes a
complete set of data as input and returns a value that is computed from all the values in
the set. E.g. max(), min(), count(), sum(), avg().
– Control Functions allow us a degree of conditionality when returning result sets
4. 03/15/2025 08:51 PM 4
الصناعة تكنولوجيا كلية
والطاقة
MySQL Aggregate Functions
• An aggregate function performs a calculation on multiple values and returns a single value.
• We mostly use the aggregate functions with SELECT statements in the data query
languages.
• The following are the aggregate functions:
• AVG() - Returns the average of the non-NULL values in the specified field.
• SUM() - Returns the sum of the non-NULL values in the specified field.
• MIN() - Returns the minimum of the non-NULL values in the specified field.
• MAX() - Returns the maximum of the non-NULL values in the specified field.
• COUNT() - Returns the number of rows containing non-NULL values in the specified field.
5. 03/15/2025 08:51 PM 5
الصناعة تكنولوجيا كلية
والطاقة
Count()
• The COUNT() function returns the number of rows that matches a specified criterion.
COUNT() Syntax:
• aggregate_expression: It specifies the column or expression whose NON-NULL values will
be counted
6. 03/15/2025 08:51 PM 6
الصناعة تكنولوجيا كلية
والطاقة
Example
Calculates the total number of employees name available in the table:
7. 03/15/2025 08:51 PM 7
الصناعة تكنولوجيا كلية
والطاقة
Example
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
This statement uses the COUNT(distinct expression) function that counts the Non-Null and
distinct rows in the column emp_age:
8. 03/15/2025 08:51 PM 8
الصناعة تكنولوجيا كلية
والطاقة
Sum()
• The SUM() function returns the total sum of a numeric column.
SUM() Syntax
9. 03/15/2025 08:51 PM 9
الصناعة تكنولوجيا كلية
والطاقة
Example
Execute the following query that calculates the total number of working hours of all employees
10. 03/15/2025 08:51 PM 10
الصناعة تكنولوجيا كلية
والطاقة
Example
MySQL sum() function with WHERE clause
11. 03/15/2025 08:51 PM 11
الصناعة تكنولوجيا كلية
والطاقة
AVG()
• The AVG() function returns the average value of a numeric column.
AVG() Syntax
12. 03/15/2025 08:51 PM 12
الصناعة تكنولوجيا كلية
والطاقة
Example
mysql> SELECT AVG(working_hours) AS Avg_working_hours FROM employees;
13. 03/15/2025 08:51 PM 13
الصناعة تكنولوجيا كلية
والطاقة
Example
mysql> SELECT AVG(working_hours) AS
Avg_working_hours FROM employees WHERE working_hours>=12;
mysql> SELECT emp_name, occupation, AVG(working_hours) AS
Avg_working_hours FROM employees GROUP BY occupation;
14. 03/15/2025 08:51 PM 14
الصناعة تكنولوجيا كلية
والطاقة
MIN() and MAX()
• The MIN() function returns the smallest value of the selected column.
• The MAX() function returns the largest value of the selected column.
MIN() Syntax
MAX() Syntax
17. 03/15/2025 08:51 PM 17
الصناعة تكنولوجيا كلية
والطاقة
Control Functions
The control functions that allow us a degree of conditionality
when returning result sets
18. 03/15/2025 08:51 PM 18
الصناعة تكنولوجيا كلية
والطاقة
IF() Function
The IF function returns a value YES when the given condition evaluates to
true and returns a NO value when the condition evaluates to false
28. 03/15/2025 08:51 PM 28
الصناعة تكنولوجيا كلية
والطاقة
String Function
• String values can be explained as “bits of text”. The string functions
allow us to manipulate these values before they are displayed.
• Adding text to an existing value
• Changing part of a string
• Extracting text from a string
• Finding a piece of text in string
29. 03/15/2025 08:51 PM 29
الصناعة تكنولوجيا كلية
والطاقة
CONCAT()
• Adding text to an existing value
30. 03/15/2025 08:51 PM 30
الصناعة تكنولوجيا كلية
والطاقة
REPLACE
• Changing Part of a String
47. 03/15/2025 08:51 PM 47
الصناعة تكنولوجيا كلية
والطاقة
User Defined Function
• MYSQL also supports user defined functions that extend MYSQL.
• User define functions are functions that you can create using a
programming language such as C, C++ etc., and then add them to MYSQL
server.
• Once added, they can be used just like any other function.