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.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read