---Advertisement---

MySQL Aggregate Functions: A Complete Guide Best 2025

By Manisha

Updated On:

---Advertisement---
MySQL Aggregate Functions

MySQL Aggregate Functions: Aggregate functions in MySQL allow us to perform calculations on multiple rows of a single column and return a single summarized value. These functions are commonly used for reporting and analytics.

The ISO standard defines five primary aggregate functions in MySQL:

  1. COUNT() – Counts the number of rows.
  2. SUM() – Calculates the total sum of values.
  3. AVG() – Computes the average value.
  4. MIN() – Retrieves the minimum value.
  5. MAX() – Retrieves the maximum value.

Why Use Aggregate Functions?

Aggregate functions simplify data analysis by summarizing large datasets. Some common use cases include:

  • Finding the most and least rented movies in a rental database.
  • Calculating the average sales per month.
  • Getting the highest and lowest sales figures for business analysis.

MySQL Aggregate Functions: The COUNT() function returns the number of values in a specific column. It works on both numeric and non-numeric data types.

Example: Count Movie Rentals

Find the total number of times a specific movie (with movie_id = 2) has been rented out:

sql

SELECT COUNT(movie_id) FROM movierentals WHERE movie_id = 2;

🔹 Result: The query will return the count of rentals for movie_id = 2.

Unlike COUNT(column_name), COUNT(*) includes null and duplicate values:

sql

SELECT COUNT(*) FROM movierentals;


MySQL Aggregate Functions: The DISTINCT keyword ensures duplicate values are not counted.

Example: Count Unique Movies Rented

sql

SELECT COUNT(DISTINCT movie_id) FROM movierentals;

🔹 Result: The number of unique movies rented out.


The MIN() function returns the smallest value in a column.

Example: Find the Oldest Movie Release Year

sql

SELECT MIN(year_released) FROM movies;

🔹 Result: The earliest year a movie was released.


The MAX() function returns the highest value in a column.

Example: Find the Latest Movie Release Year

sql

SELECT MAX(year_released) FROM movies;

🔹 Result: The most recent movie release year.


The SUM() function adds up all values in a numeric column.

Example: Calculate Total Payments Made

sql

SELECT SUM(amount_paid) FROM payments;

🔹 Result: The total amount of payments made by customers.


The AVG() function calculates the average of numeric values.

Example: Find the Average Payment Amount

sql

SELECT AVG(amount_paid) FROM payments;

🔹 Result: The average payment amount.


Aggregate functions are often used with GROUP BY to group results based on a column.

Example: Calculate Total Payments Per Customer

sql

SELECT membership_number, SUM(amount_paid) 

FROM payments 

GROUP BY membership_number;

🔹 Result: Returns the total amount paid by each customer.


MySQL Aggregate Functions are essential for database analytics and reporting. Key takeaways:
COUNT, SUM, AVG, MIN, and MAX are the five standard aggregate functions.
✅ Use DISTINCT to remove duplicates from aggregate results.
GROUP BY helps group data before applying aggregate functions.

By mastering aggregate functions, you can efficiently summarize and analyze data in MySQL!

Complete Guide To MYSQL Function

Download My SQL

Leave a Comment

Index