Aggregate Functions
In this session, you will learn:
• What is an Aggregate Functions?
• List of Aggregate Functions with example queries
Aggregate Functions
• An aggregate function performs a calculation on a set of
values and returns a single value.
✓ Count()
✓ Sum()
✓ Avg()
✓ Max()
✓ Min()
Aggregate Function – Count()
• returns the number of rows present in the table either based
on some condition or without condition.
Example
SELECT count(*) AS Customer_Count
from Customer WHERE City IN (‘Chennai’,’CBE’);
Customer
Customer
Count(Customer_Id)
Customer_Count
3
Aggregate Function – Count()
Example
SELECT count(DISTINCT City) AS City_Count
from Customer
Customer Customer
City_Count
4
Aggregate Function – Sum()
• returns total sum of a selected columns numeric values.
Example
SELECT Sum(Price) AS Total_Price
from Product
Product
Product Total_Price
14500
Aggregate Function – Avg()
• Calculates the average value of a column of numeric type.
Example
SELECT Avg(Price) AS Average_Price
from Product
Product
Product Average_Price
4833.3333
Aggregate Function – Min()
• returns minimum value from a selected column of the table
Example
SELECT Min(Price) AS Minimum_Price
from Product
Product
Product Minimum_Price
1500
Aggregate Function – Max()
• returns maximum value from a selected column of the table
Example
SELECT Max(Price) AS Maximum_Price
from Product
Product
Product Maximum_Price
8000
GROUP BY clause
• GROUP BY clause is used with SELECT statement to collect
data from multiple records and group the results by one or more
columns.
Syntax
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n;
GROUP BY clause
• expression1, expression2, ... expression_n: It specifies the expressions
that are not encapsulated within aggregate function. These
expressions must be included in GROUP BY clause.
• aggregate_function: It specifies the aggregate functions i.e. SUM,
COUNT, MIN, MAX or AVG functions
• aggregate_expression: It specifies the column or expression on that
the aggregate function is based on.
• tables: It specifies the table from where you want to retrieve records.
• conditions: It specifies the conditions that must be fulfilled for the
record to be selected.
GROUP BY clause
Example 1
SELECT Pdt_Type, Max(Price) AS Maximum_Price
from Product GROUP BY Pdt_Type ORDER BY Pdt_Type;
Product
Product Pdt_Type Maximum_Price
Books 1500
Electronics 8000
Men 5000
Apparel
GROUP BY clause
Example 1
SELECT Product_Id, Pdt_Type, Max(Price) AS Maximum_Price
from Product GROUP BY Pdt_Type ORDER BY Pdt_Type;
Product Error.
Because the select
Expression Product_Id
is missing in GROUP BY
Ensure that all of the GROUP BY columns match the SELECT clause.
having_conditions: It specifies the conditions that are applied only to the
aggregated results to restrict the groups of returned rows.
The GROUP BY clause is used in the SELECT statement for grouping the rows
by values of column or expression. For example, given groups of products in
several categories, the AVG() function returns the average price of products
in each category
THANKS