Skip to main content
Practice

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

FunctionPurposeExample Usage
SUM()Adds up values in a columnSUM(salary)
COUNT()Counts rows or non-null valuesCOUNT(*), COUNT(name)
AVG()Calculates the average valueAVG(grade)
MAX()Finds the highest valueMAX(score)
MIN()Finds the lowest valueMIN(price)

These are all used inside a SELECT statement.


===

3. Example: Total Sales

Assume you have a table called sales with the following data:

idamount
1100
2200
3300
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.