0% found this document useful (0 votes)
337 views

Aggregate Funtion in SQL DB Presentation

Aggregate functions in SQL allow users to perform calculations on multiple rows and columns of data and return a single value. Some common aggregate functions include COUNT, MAX, MIN, SUM, and AVG. These functions allow users to count rows, find minimum or maximum values, calculate sums and averages. The document provides examples of how to use each function, such as counting the number of times a movie was rented, finding the earliest or latest year a movie was released, summing all payment amounts, and averaging payment amounts.

Uploaded by

Zahra Batool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views

Aggregate Funtion in SQL DB Presentation

Aggregate functions in SQL allow users to perform calculations on multiple rows and columns of data and return a single value. Some common aggregate functions include COUNT, MAX, MIN, SUM, and AVG. These functions allow users to count rows, find minimum or maximum values, calculate sums and averages. The document provides examples of how to use each function, such as counting the number of times a movie was rented, finding the earliest or latest year a movie was released, summing all payment amounts, and averaging payment amounts.

Uploaded by

Zahra Batool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

AGGREGATE

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

SELECT SUM(`amount_paid`) FROM `payments`;

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

You might also like