Aggregate Funtion in SQL DB Presentation
Aggregate Funtion in SQL DB Presentation
FUNCTIONS IN SQL
Name Zahra Batool
Roll No 091
Aggregate Functions
In database management, an aggregate function or aggregation function is
a function where the values of multiple rows are grouped together to form
a single summary value
Some of the most common aggregate functions include:
Average (expression ) Calculate the average of the expression
Count(expression ) Count occurrences of non-null values returned
by the expression.
.
Count (*) Count all rows in the specified table.
Max (expression ) Finds the maximum expression value.
Min (expression ) Finds the maximum expression value.
Sum (expression ) Calculate the sum of the expression.
General Syntax of an Aggregate Function
When using the aggregate function you can either compute the result on all values
or distinct values.
For instance, to count all OrderDetails records we could use the expression:
SELECT COUNT(OrderID) FROM OrderDetails;
To count the distinct of orders making up the details we would use the following:
SELECT COUNT(DISTINCT OrderID) FROM OrderDetails;
COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
Let's suppose that we want to get the number of times that the movie with id 2 has been
rented out
SELECT COUNT(`movie_id`) FROM `movierentals` WHERE `movie_id` = 2;
Output:
MIN function
The MIN function returns the smallest value in the specified table field.
The following query helps us achieve that:
SELECT MIN(`reference-number`) FROM `movies`;
Output:
reference-number
11
MAX function
Just as the name suggests, the MAX function is the opposite of the MIN function.
It returns the largest value from the specified table field.
The following example returns the latest movie year released.
SELECT MAX(`year_released`) FROM `movies`;
20099
20069
20049
20129
20059
Output:
SUM function
SUM function returns the sum of all the values in the specified column. SUM works
on numeric fields only. Null values are excluded from the result returned.
The following table shows the data in payments table
Output:
AVG function
MySQL AVG function returns the average of the values in a specified column. Just
like the SUM function, it works only on numeric data types.
Suppose we want to find the average amount paid.
We can use the following query -
SELECT AVG(`amount_paid`) FROM `payments`;
Output:
Thank
you