In this tutorial, I am going to explain about aggregate functions in MySQL with examples. Without wasting time, let’s start.
Aggregate - Constituting or amounting to a whole
Aggregate functions describe operations on a set of values, like as counting, averaging, or finding the minimum or maximum values. Aggregate functions assist with the restating of large volumes of data. The aggregate functions COUNT, SUM, AVG, MAX, MIN, and LIST do not handle NULL in the same way as ordinary functions and operators. Aggregate functions perform a calculation on a set of values and return a single value.
Aggregate functions are used in the act of joining with a GROUP BY clause to dispose of values from a result set into groups.
There are some functions that operate on sets of values.
- MIN() and MAX() : find smallest and largest values.
- SUM(): summarize numeric values to produce sums (totals).
- COUNT(): counts rows, values, or the number of distinct values.
- AVG() :calculate the average, used with numeric values.
Syntax
function_name (DISTINCT | ALL Expression)
Aggregate functions may be used with or without a GROUP BY clause that places rows into groups. Without a GROUP BY clause, an aggregate function calculates a summary value based on the entire set of selected rows. MySQL behaves with all the rows as a single group. With a GROUP BY clause, an aggregate function calculates a summary value for each group.
Without wasting time, let’s create a database and some tables on which we have to apply aggregate functions.
Create a Database
- CREATE DATABASE Aggregate_Functions;
Create a Library Table
- USE Aggregate_Functions;
-
- CREATE TABLE Library (
- BookNumber int NOT NULL,
- BookCode varchar(15) NOT NULL,
- BookIssue int NOT NULL,
- CostEach decimal(10,2) NOT NULL,
- PRIMARY KEY (BookNumber,BookCode)
- );
Now, insert some values into it.
- INSERT INTO Library(BookNumber, BookCode, BookIssue, CostEach) VALUES
- (10100, 'B18_1749', 30, '136.00'),
- (10100, 'B18_2248', 50, '55.09'),
- (10101, 'B18_2325', 25, '108.06'),
- (10101, 'B18_2795', 26, '167.06'),
- (10102, 'B18_1342', 39, '95.55'),
- (10102, 'B18_1367', 41, '43.13'),
- (10103, 'B10_1949', 26, '214.30'),
- (10103, 'B10_4962', 42, '119.67'),
- (10103, 'B18_2432', 22, '58.34'),
- (10103, 'B18_2949', 27, '92.19'),
- (10104, 'B18_3232', 23, '165.95'),
- (10104, 'B18_4027', 38, '119.20'),
- (10104, 'B24_1444', 35, '52.02'),
- (10104, 'B24_2840', 44, '30.41'),
- (10104, 'B24_4048', 26, '106.45'),
- (10105, 'B24_2011', 43, '117.97'),
- (10105, 'B24_3151', 44, '73.46'),
- (10105, 'B24_3816', 50, '75.47'),
- (10105, 'B700_1138', 41, '54.00'),
- (10105, 'B700_1938', 29, '86.61'),
- (10106, 'B18_3856', 41, '94.22'),
- (10106, 'B24_1785', 28, '107.23'),
- (10106, 'B24_2841', 49, '65.77'),
- (10106, 'B24_3420', 31, '55.89'),
- (10106, 'B24_3949', 50, '55.96'),
- (10107, 'B12_2823', 21, '122.00'),
- (10107, 'B18_2625', 29, '52.70'),
- (10107, 'B24_1578', 25, '96.92'),
- (10107, 'B24_2000', 38, '73.12'),
- (10107, 'B32_1374', 20, '88.90'),
- (10108, 'B24_4620', 31, '67.10'),
- (10108, 'B32_2206', 27, '36.21'),
- (10108, 'B32_4485', 31, '87.76'),
- (10108, 'B50_4713', 34, '74.85'),
- (10109, 'B18_1129', 26, '117.48'),
- (10109, 'B18_1984', 38, '137.98'),
- (10109, 'B18_2870', 26, '126.72'),
- (10109, 'B18_3232', 46, '160.87'),
- (10110, 'B24_2887', 46, '112.74'),
- (10110, 'B24_3191', 27, '80.47'),
- (10110, 'B24_3432', 37, '96.37'),
- (10110, 'B24_3969', 48, '35.29'),
- (10111, 'B18_1342', 33, '87.33'),
- (10111, 'B18_1367', 48, '48.52'),
- (10111, 'B18_2957', 28, '53.09'),
- (10111, 'B18_3136', 43, '94.25'),
- (10112, 'B10_1949', 29, '197.16'),
- (10112, 'B18_2949', 23, '85.10'),
- (10113, 'B12_1666', 21, '121.64'),
- (10113, 'B18_1097', 49, '101.50'),
- (10113, 'B18_4668', 50, '43.27'),
- (10113, 'B32_3522', 23, '58.82'),
- (10114, 'B10_4962', 31, '128.53'),
- (10114, 'B18_2319', 39, '106.78'),
- (10114, 'B18_2432', 45, '53.48'),
- (10114, 'B18_3232', 48, '169.34'),
- (10115, 'B18_2238', 46, '140.81'),
- (10115, 'B24_1444', 47, '56.64'),
- (10115, 'B24_4048', 44, '106.45'),
- (10115, 'B50_1392', 27, '100.70'),
- (10116, 'B32_3207', 27, '60.28'),
- (10117, 'B12_1108', 33, '195.33'),
- (10117, 'B12_3148', 43, '148.06'),
- (10117, 'B12_3891', 39, '173.02'),
- (10117, 'B18_3140', 26, '121.57'),
- (10118, 'B700_3505', 36, '86.15'),
- (10119, 'B10_4757', 46, '112.88'),
- (10119, 'B18_1662', 43, '151.38'),
- (10119, 'B18_3029', 21, '74.84'),
- (10119, 'B18_3856', 27, '95.28'),
- (10120, 'B700_1691', 47, '91.34'),
- (10120, 'B700_2466', 24, '81.77'),
- (10120, 'B700_2834', 24, '106.79'),
- (10120, 'B700_3167', 43, '72.00'),
- (10121, 'B10_1678', 34, '86.13'),
- (10121, 'B12_2823', 50, '126.52'),
- (10121, 'B24_2360', 32, '58.18'),
- (10121, 'B32_4485', 25, '95.93'),
- (10121, 'B50_4713', 44, '72.41');
Create a Books Table
- CREATE TABLE Books (
- BookCode varchar(50) NOT NULL,
- BookName varchar(100) NOT NULL,
- BookType varchar(100) NOT NULL,
- BookVendor varchar(100) NOT NULL,
- BookDescription varchar(250),
- BookInStock int NOT NULL,
- Cost decimal(10,2) NOT NULL,
- PRIMARY KEY (BookCode)
- ) ;
Now, insert some records into the Books Table
- INSERT INTO Books(BookCode, BookName, BookType, BookVendor, BookDescription, BookInStock, Cost) VALUES
- ('B10_001', 'Coding Fundamentals - C', 'Core Book', 'Min Lin Diecast', null, 7933, '48.81'),
- ('B10_002', 'Coding Fundamentals - C++', 'Core Book', 'Classic Metal Creations', null, 7305, '98.58'),
- ('B10_003', 'Coding Fundamentals - C#', 'Advanced Book', 'Mini Classics', null, 6625, '68.99'),
- ('B10_004', 'Coding Fundamentals - PHP', 'E-Book', 'Start Diecast', null, 5582, '91.02'),
- ('B10_005', 'Coding Fundamentals - Java', 'E-Book', 'City Art Classics', null, 3252, '85.68'),
- ('B10_006', 'Coding Fundamentals - JavaScript', 'E-Book', 'Second Diecast', null, 6791, '103.42'),
- ('B12_007', 'Coding Fundamentals - Angular', 'Advanced Book', 'Autoart Studio Design', null, 68, '95.34'),
- ('B12_008', 'Coding Fundamentals - R', 'E-Book', 'Second Diecast', null, 3619, '95.59'),
- ('B12_009', 'Coding Fundamentals - Python', 'Core Book', 'Welly Publications', null, 1579, '77.90'),
- ('B12_010', 'Coding Fundamentals - HTML', 'Core Book', 'Art Galleries', null, 9997, '66.27'),
- ('B12_011', 'Coding Fundamentals - CSS', 'Core Book', 'Welly Publications', null, 6906, '89.14'),
- ('B12_012', 'Coding Fundamentals - CSS3', 'Core Book', 'Welly Publications', null, 9123, '75.16'),
- ('B12_013', 'Coding Fundamentals - NodeJs', 'E-Book', 'Second Diecast', null, 1049, '83.05'),
- ('B12_014', 'Coding Fundamentals - SQL SERVER', 'Advanced Book', 'Studio Art Model Productions', null, 5663, '31.92'),
- ('B12_015', 'Coding Fundamentals - MySQL', 'E-Book', 'Designs Publications', null, 6125, '55.70'),
- ('B12_016', 'Coding Fundamentals - Android', 'E-Book', 'Welly Diecast Publications', null, 7323, '58.73'),
- ('B18_017', 'Coding Fundamentals - Machine Learning', 'E-Book', 'Studio Art Model Productions', null, 2613, '58.33'),
- ('B18_018', 'AI', 'Digital Book', 'Mini Classics', null, 3975, '83.51'),
- ('B18_019', 'Data Science', 'Digital Book', 'City Art Classics', null, 8693, '60.62'),
- ('B18_020', 'Coding Advanced - C', 'E-Book', 'Studio Art Model Productions', null, 8635, '24.26'),
- ('B18_021', 'Coding Advanced - C#', 'Core Book', 'Classic Creations Productions', null, 9042, '65.96'),
- ('B18_022', 'Coding Advanced - C++', 'Advanced Book', 'Start Diecast Productions', null, 5330, '77.27'),
- ('B18_023', 'Coding Advanced - JS', 'Core Book', 'Welly Publications', null, 2724, '86.70'),
- ('B18_024', 'Coding Advanced - R', 'E-Book', 'Collectibles Productions', null, 8826, '53.90'),
- ('B18_025', 'Coding Advanced - Angular', 'E-Book', 'Lin Diecast Productions', null, 9772, '93.89');
MAX() and MIN() Functions
A) MAX function returns the maximum value in a set
For Example
1) MAX function is used to get the most costly Book from a Books table.
- SELECT MAX(Cost) AS Costly_Book
- FROM books
2) MAX() aggregate function can be used with GROUP BY clause to get the highest cost for every type of book
- SELECT BookType, max(Cost) AS Costly_Book
- FROM books
- GROUP BY BookType
- ORDER BY Costly_Book ASC
B) MIN function returns the minimum value of the set of values in the expression
For Example:
1) MIN function is used to get the lowest cost of Book from a Books table
- SELECT MIN(Cost) AS CheapCost_Book
- FROM books
2) MIN() aggregate function can be used with GROUP BY clause to get the low cost for every type of book
- SELECT BookType, MIN(Cost) AS CheapCost_Book
- FROM books
- GROUP BY BookType
- ORDER BY CheapCost_Book ASC
SUM function returns the sum of all values in an expression. And, the SUM() functions ignores NULL.
For Example
1) Here, we will use the SUM() function to get the total price for every Book Order.
- SELECT BookNumber, BookCode, SUM(bookissue * costeach) AS Total_price
- FROM library
- GROUP BY BookCode
- ORDER BY Total_price DESC
2) To calculate the sum of all the books that are in stock.
- SELECT BookType, SUM(BookInStock* Cost) AS TOTAL_COST
- FROM Books
- GROUP BY BookType
AVG() function is used to calculate the average value and also it ignores the NULL value.
For Example: To calculate the average cost of each ‘BookType’
- SELECT BookType, AVG(BookInStock * Cost) AS AverageCost
- FROM Books
- GROUP BY BookType
- ORDER BY AverageCost DESC,
COUNT function returns the count of the items in expression. The COUNT() function can be used in several ways to count either rows or values.
For Example: To calculate the total number of books for each book type, use.
- SELECT BookType, COUNT(*)
- FROM Books
- GROUP BY BookType
- ORDER BY BookType
In this article, I have discussed the concept of Functions in MySQL with various examples.
I hope you enjoyed this article. Follow C# Corner to learn more new and amazing things about MySQL.
Thanks for reading this article!