Aggregate Function Notes
Aggregate Function Notes
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.
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:
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:
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:
R(OrderID, CustomerID)
Then, we can use the following relational algebra expression to get the count of orders
placed by each customer:
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.
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:
R(CourseID, Grade)
Then, we can use the following relational algebra expression to get the average grade for
each course:
π CourseID, AVG(Grade)(R)
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.
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.