Open In App

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
Article Tags :

Similar Reads