1
deck
1. What Are Aggregate Functions?
Aggregate functions return a single value by processing multiple rows of data.
They're used in reports, dashboards, and any place where you need summaries like totals, averages, or counts.
2. Common Aggregate Functions
Function | Purpose | Example Usage |
---|---|---|
SUM() | Adds up values in a column | SUM(salary) |
COUNT() | Counts rows or non-null values | COUNT(*) , COUNT(name) |
AVG() | Calculates the average value | AVG(grade) |
MAX() | Finds the highest value | MAX(score) |
MIN() | Finds the lowest value | MIN(price) |
These are all used inside a SELECT
statement.
===
3. Example: Total Sales
Assume you have a table called sales
with the following data:
id | amount |
---|---|
1 | 100 |
2 | 200 |
3 | 300 |
Create table
CREATE TABLE sales (
id INT PRIMARY KEY,
amount INT
);
The total sales can be calculated using the SUM()
function.
SELECT SUM(amount)
FROM sales;
This returns the total amount sold across all rows in the sales
table.
Result
600
===
4. Where Are Aggregates Used?
Aggregate functions work well with:
GROUP BY
(to break totals by category)HAVING
(to filter groups by their total or average)
We'll cover both in more detail with examples later in this chapter.
===
5. Key Use Cases
You might use aggregation to:
- Calculate total revenue
- Count users who signed up this month
- Find the lowest price in stock
- Track average order value
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.