Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Aggregate Function Notes

Aggregate functions are functions in SQL that operate on sets of values and return a single value. They are commonly used to summarize or aggregate data by performing calculations on groups of rows or columns. Some common aggregate functions include COUNT, SUM, AVG, MAX, and MIN. Aggregate functions are usually used with the GROUP BY clause to group data and calculate aggregate statistics for each group.

Uploaded by

Adotua Matha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Aggregate Function Notes

Aggregate functions are functions in SQL that operate on sets of values and return a single value. They are commonly used to summarize or aggregate data by performing calculations on groups of rows or columns. Some common aggregate functions include COUNT, SUM, AVG, MAX, and MIN. Aggregate functions are usually used with the GROUP BY clause to group data and calculate aggregate statistics for each group.

Uploaded by

Adotua Matha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Aggregate functions are functions in SQL that operate on a set of values and return a

single value. These functions are commonly used in SQL queries to summarize or aggregate
data.

In relational algebra, aggregate functions are represented using the Greek letter sigma (∑).
The sigma notation represents a function that takes a set of values as input and returns a
single value. For example, the sum function (∑) takes a set of values and returns their sum.

In SQL, aggregate functions are used in the SELECT statement to compute summary
statistics on a set of data. Some common aggregate functions in SQL include:

COUNT: This function counts the number of rows in a table or a group of rows.

SUM: This function calculates the sum of a column or a group of rows.

AVG: This function calculates the average of a column or a group of rows.

MAX: This function returns the maximum value of a column or a group of rows.

MIN: This function returns the minimum value of a column or a group of rows.

Aggregate functions are usually used in conjunction with the GROUP BY clause in SQL to
group data based on one or more columns and compute summary statistics for each group.
For example, the following SQL query computes the average salary for each department in a
company:
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
This query groups the employees by their department and computes the average salary for
each group. The result is a table that shows the department name and the corresponding
average salary for that department.
 In relational algebra, aggregate functions are used to perform calculations on groups of
tuples. The syntax for aggregate functions is as follows:

Γ (G, F(R))
where:

Γ is the symbol for the grouping operation


G is a list of attributes used for grouping
F is a list of aggregate functions to be applied
R is the relation being operated on
The aggregate functions that can be used in relational algebra include:

 SUM
 COUNT
 AVG
 MIN
 MAX

For example, suppose we have a relation called "Sales" with the following attributes:
"Salesperson", "Product", and "Amount". We can use relational algebra to calculate the
total sales amount for each salesperson:

Γ (Salesperson, SUM(Amount))(Sales)
This will group the sales by salesperson and then calculate the sum of the amounts for each
group.
Here are a few examples of using aggregate functions in relational algebra:

 Calculate the average salary of employees in the "Employees" relation:


Γ (AVG(Salary))(Employees)

 Count the number of orders for each customer in the "Orders" relation:
Γ (Customer, COUNT(Order_ID))(Orders)

 Find the minimum and maximum prices of products in the "Products" relation:
Γ (MIN(Price), MAX(Price))(Products)

 Calculate the total revenue and average revenue per order for a restaurant in the
"Orders" relation:
Γ (SUM(Total_Price), AVG(Total_Price))(Orders)

In all of these examples, the aggregate functions are applied to groups of tuples in the given
relations to calculate aggregate values.
 Here's an example of using the COUNT aggregate function in relational algebra:

Consider a table named "Sales" with the following attributes:

OrderID CustomerID OrderDate TotalSale


1 101 2022-01-01 100
2 101 2022-01-05 50
3 102 2022-01-07 75
4 103 2022-01-08 200
5 102 2022-01-10 125
We want to find out the number of orders placed by each customer. To do this, we can use
the COUNT aggregate function with the GROUP BY clause in relational algebra. Here's the
solution:

Let's define a relation R with the following schema:

R(OrderID, CustomerID)

R = Sales {OrderID, CustomerID}

Then, we can use the following relational algebra expression to get the count of orders
placed by each customer:

π CustomerID, COUNT(OrderID)(σ R.CustomerID = Sales.CustomerID (R ⋈ Sales))

Here's what this expression does step by step:

Join relation R with Sales using the CustomerID attribute to get all the orders placed by each
customer.

Apply the selection (σ) operator to select only the tuples where the CustomerID in R
matches the CustomerID in Sales.

Apply the projection (π) operator to project only the CustomerID and the count of OrderID
for each customer.

The resulting table would be:

CustomerID COUNT(OrderID)
101 2
102 2
103 1
This table shows that customer 101 has placed 2 orders, customer 102 has placed 2 orders,
and customer 103 has placed 1 order.
 Here's an example of using the AVERAGE (AVG) aggregate function in relational algebra:

Consider a table named "Grades" with the following attributes:

StudentID CourseID Grade


1 1 75
1 2 85
2 1 90
2 2 95
3 1 80
3 2 70
We want to find the average grade for each course. To do this, we can use the AVG
aggregate function with the GROUP BY clause in relational algebra. Here's the solution:

Let's define a relation R with the following schema:

R(CourseID, Grade)

R = Grades {CourseID, Grade}

Then, we can use the following relational algebra expression to get the average grade for
each course:

π CourseID, AVG(Grade)(R)

Here's what this expression does step by step:

Apply the projection (π) operator to select only the CourseID and Grade attributes.

Apply the AVG aggregate function to calculate the average grade for each group of tuples
with the same CourseID.

The resulting table would be:

CourseID AVG(Grade)
1 81.67
2 83.33
This table shows that the average grade for course 1 is 81.67 and the average grade for
course 2 is 83.33.

You might also like