Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PL/SQL Aggregate Function
Next article icon

MySQL Aggregate Function

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

MySQL Aggregate Functions are used to calculate values from multiple rows and return a single result, helping in summarizing and analyzing data. They include functions for counting, summing, averaging, and finding maximum or minimum values, often used with the GROUP BY clause. In this article, we will see different aggregate functions.

MySQL Aggregate Functions

Aggregate functions in MySQL process a set of values and return a single value. They are typically used with the SELECT statement and can be applied to various columns within a table. Common aggregate functions include COUNT(), SUM(), AVG(), MAX(), and MIN(). These functions provide essential tools for summarizing and analyzing data efficiently.

Syntax:

SELECT AGGREGATE_FUNCTION(column_name)
FROM table_name
WHERE condition;

Where,

  • AGGREGATE_FUNCTION(): The aggregate function you want to use (e.g., COUNT, SUM).
  • column_name: The column on which the function is applied.
  • table_name: The name of the table from which to retrieve the data.
  • condition: An optional WHERE clause to filter the rows.

Demo Database

let's create a sample database to demonstrate the use of MySQL Aggregate Functions. We'll create an employees table:

CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);

INSERT INTO employees (name, department, salary) VALUES
('Amit', 'Sales', 50000.00),
('Neha', 'HR', 60000.00),
('Ravi', 'IT', 70000.00),
('Priya', 'Sales', 55000.00),
('Sandeep', 'IT', 65000.00);

Common Aggregate Functions

COUNT()

The COUNT() function returns the number of rows that match a specified condition. It can count all rows or only rows that meet certain criteria.

Example:

SELECT COUNT(*) AS total_employees FROM employees;

Output:

+-----------------+
| total_employees |
+-----------------+
| 5 |
+-----------------+

Explanation: This query counts all rows in the 'employees' table and returns the total number of employees.

SUM()

The SUM() function returns the total sum of a numeric column.

Example:

SELECT SUM(salary) AS total_sales FROM employees;

Output:

+-------------+
| total_sales |
+-------------+
| 300000.00 |
+-------------+

Explanation: This query sums up all values in the 'salary' column of the 'employees' table and returns the total employees salary.

AVG()

The AVG() function returns the average value of a numeric column.

Example:

SELECT AVG(salary) AS average_salary FROM employees;

Output:

+----------------+
| average_salary |
+----------------+
| 60000.000000 |
+----------------+

Explanation: This query calculates the average value of the 'salary' column in the 'employees' table and returns the average salary.

MAX()

The MAX() function returns the maximum value in a set of values.

Example:

SELECT MAX(salary) AS highest_salary FROM employees;

Output:

+----------------+
| highest_salary |
+----------------+
| 70000.00 |
+----------------+

Explanation: This query finds the maximum value in the 'salary' column of the 'employees' table and returns the highest salary.

MIN()

The MIN() function returns the minimum value in a set of values.

Example:

SELECT MIN(salary) AS lowest_salary FROM employees;

Output:

+---------------+
| lowest_salary |
+---------------+
| 50000.00 |
+---------------+

Explanation: This query finds the minimum value in the 'salary' column of the 'employees' table and returns the lowest salary.

Conclusion

MySQL Aggregate Functions like COUNT(), SUM(), AVG(), MAX(), and MIN() are powerful tools for data summarization and analysis. They allow you to perform complex calculations and derive meaningful insights from large datasets. Understanding how to use these functions effectively can enhance your data analysis capabilities.


Next Article
PL/SQL Aggregate Function
author
yuvrajghule281
Improve
Article Tags :
  • Databases
  • MySQL

Similar Reads

  • SQL Aggregate functions
    SQL Aggregate Functions are used to perform calculations on a set of rows and return a single value. These functions are particularly useful when we need to summarize, analyze, or group large datasets in SQL databases. Whether you're working with sales data, employee records, or product inventories,
    4 min read
  • PL/SQL Aggregate Function
    In PL/SQL, aggregate functions play an important role in summarizing and analyzing data from large datasets. These built-in SQL functions perform calculations on a set of values and return a single result, making them invaluable for tasks like calculating totals, averages, and identifying the highes
    6 min read
  • SQLAlchemy - Aggregate Functions
    In this article, we will see how to select the count of rows using SQLAlchemy using Python. Before we begin, let us install the required dependencies using pip: pip install sqlalchemySince we are going to use MySQL in this post, we will also install a SQL connector for MySQL in Python. However, none
    4 min read
  • SQL AVG() Function
    The AVG() function in SQL is an aggregate function used to calculate the average (mean) value of a numeric column in a table. It provides insights into the central tendency of numerical data, making it an essential tool for statistical analysis and reporting. The function automatically excludes NULL
    4 min read
  • EXTRACT() Function in MySQL
    The EXTRACT() function in MySQL is a versatile tool used for retrieving specific components of date Whether we need the year, month, day or even the hour or minute. This function simplifies date manipulation and makes queries involving date and time data more efficient and easier to understand. In t
    3 min read
  • PL/SQL AVG() Function
    The PL/SQL AVG() function serves as a powerful tool for performing aggregate calculations on numeric datasets within a database. By allowing developers to calculate average values while excluding NULL entries, it enhances data analysis capabilities. In this article, we will explore the AVG() functio
    5 min read
  • AVG() Function in SQL
    SQL is an RDBMS system in which SQL functions become very essential to provide us with primary data insights. One of the most important functions is called AVG() and is particularly useful for the calculation of averages within datasets. In this, we will learn about the AVG() function, and its synta
    4 min read
  • Using SQLite Aggregate functions in Python
    In this article, we are going to see how to use the aggregate function in SQLite Python. An aggregate function is a database management function that groups the values of numerous rows into a single summary value. Average (i.e., arithmetic mean), sum, max, min, Count are common aggregation functions
    4 min read
  • How to Use aggregate Function in R
    In this article, we will discuss how to use aggregate function in R Programming Language. aggregate() function is used to get the summary statistics of the data by group. The statistics include mean, min, sum. max etc. Syntax: aggregate(dataframe$aggregate_column, list(dataframe$group_column), FUN)
    2 min read
  • COUNT() Function in MySQL
    The COUNT() function in MySQL is a versatile aggregate function used to determine the number of rows or non-NULL values that match a specific condition in a query. It can be applied to an entire table or a particular column and is widely used in database operations to analyze the volume of data in a
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences