Aggregate Functions in MySQL

Introduction

 
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. 
 
1
 
Without wasting time, let’s create a database and some tables on which we have to apply aggregate functions.
 
Create a Database
  1. CREATE DATABASE Aggregate_Functions; 
Create a Library Table
  1. USE Aggregate_Functions;  
  2.    
  3. CREATE TABLE Library (  
  4.   BookNumber int NOT NULL,  
  5.   BookCode varchar(15) NOT NULL,  
  6.   BookIssue int NOT NULL,  
  7.   CostEach decimal(10,2) NOT NULL,  
  8.   PRIMARY KEY (BookNumber,BookCode)  
  9. ); 
Now, insert some values into it.
  1. INSERT INTO Library(BookNumber, BookCode, BookIssue, CostEach) VALUES    
  2. (10100, 'B18_1749', 30, '136.00'),    
  3. (10100, 'B18_2248', 50, '55.09'),    
  4. (10101, 'B18_2325', 25, '108.06'),    
  5. (10101, 'B18_2795', 26, '167.06'),    
  6. (10102, 'B18_1342', 39, '95.55'),    
  7. (10102, 'B18_1367', 41, '43.13'),    
  8. (10103, 'B10_1949', 26, '214.30'),    
  9. (10103, 'B10_4962', 42, '119.67'),    
  10. (10103, 'B18_2432', 22, '58.34'),    
  11. (10103, 'B18_2949', 27, '92.19'),    
  12. (10104, 'B18_3232', 23, '165.95'),    
  13. (10104, 'B18_4027', 38, '119.20'),    
  14. (10104, 'B24_1444', 35, '52.02'),    
  15. (10104, 'B24_2840', 44, '30.41'),    
  16. (10104, 'B24_4048', 26, '106.45'),    
  17. (10105, 'B24_2011', 43, '117.97'),    
  18. (10105, 'B24_3151', 44, '73.46'),    
  19. (10105, 'B24_3816', 50, '75.47'),    
  20. (10105, 'B700_1138', 41, '54.00'),    
  21. (10105, 'B700_1938', 29, '86.61'),    
  22. (10106, 'B18_3856', 41, '94.22'),    
  23. (10106, 'B24_1785', 28, '107.23'),    
  24. (10106, 'B24_2841', 49, '65.77'),    
  25. (10106, 'B24_3420', 31, '55.89'),    
  26. (10106, 'B24_3949', 50, '55.96'),    
  27. (10107, 'B12_2823', 21, '122.00'),    
  28. (10107, 'B18_2625', 29, '52.70'),    
  29. (10107, 'B24_1578', 25, '96.92'),    
  30. (10107, 'B24_2000', 38, '73.12'),    
  31. (10107, 'B32_1374', 20, '88.90'),    
  32. (10108, 'B24_4620', 31, '67.10'),    
  33. (10108, 'B32_2206', 27, '36.21'),    
  34. (10108, 'B32_4485', 31, '87.76'),    
  35. (10108, 'B50_4713', 34, '74.85'),    
  36. (10109, 'B18_1129', 26, '117.48'),    
  37. (10109, 'B18_1984', 38, '137.98'),    
  38. (10109, 'B18_2870', 26, '126.72'),    
  39. (10109, 'B18_3232', 46, '160.87'),    
  40. (10110, 'B24_2887', 46, '112.74'),    
  41. (10110, 'B24_3191', 27, '80.47'),    
  42. (10110, 'B24_3432', 37, '96.37'),    
  43. (10110, 'B24_3969', 48, '35.29'),    
  44. (10111, 'B18_1342', 33, '87.33'),    
  45. (10111, 'B18_1367', 48, '48.52'),    
  46. (10111, 'B18_2957', 28, '53.09'),    
  47. (10111, 'B18_3136', 43, '94.25'),    
  48. (10112, 'B10_1949', 29, '197.16'),    
  49. (10112, 'B18_2949', 23, '85.10'),    
  50. (10113, 'B12_1666', 21, '121.64'),    
  51. (10113, 'B18_1097', 49, '101.50'),    
  52. (10113, 'B18_4668', 50, '43.27'),    
  53. (10113, 'B32_3522', 23, '58.82'),    
  54. (10114, 'B10_4962', 31, '128.53'),    
  55. (10114, 'B18_2319', 39, '106.78'),    
  56. (10114, 'B18_2432', 45, '53.48'),    
  57. (10114, 'B18_3232', 48, '169.34'),    
  58. (10115, 'B18_2238', 46, '140.81'),    
  59. (10115, 'B24_1444', 47, '56.64'),    
  60. (10115, 'B24_4048', 44, '106.45'),    
  61. (10115, 'B50_1392', 27, '100.70'),    
  62. (10116, 'B32_3207', 27, '60.28'),    
  63. (10117, 'B12_1108', 33, '195.33'),    
  64. (10117, 'B12_3148', 43, '148.06'),    
  65. (10117, 'B12_3891', 39, '173.02'),    
  66. (10117, 'B18_3140', 26, '121.57'),    
  67. (10118, 'B700_3505', 36, '86.15'),    
  68. (10119, 'B10_4757', 46, '112.88'),    
  69. (10119, 'B18_1662', 43, '151.38'),    
  70. (10119, 'B18_3029', 21, '74.84'),    
  71. (10119, 'B18_3856', 27, '95.28'),    
  72. (10120, 'B700_1691', 47, '91.34'),    
  73. (10120, 'B700_2466', 24, '81.77'),    
  74. (10120, 'B700_2834', 24, '106.79'),    
  75. (10120, 'B700_3167', 43, '72.00'),    
  76. (10121, 'B10_1678', 34, '86.13'),    
  77. (10121, 'B12_2823', 50, '126.52'),    
  78. (10121, 'B24_2360', 32, '58.18'),    
  79. (10121, 'B32_4485', 25, '95.93'),    
  80. (10121, 'B50_4713', 44, '72.41');  
 
Create a Books Table
  1. CREATE TABLE Books (  
  2.   BookCode varchar(50) NOT NULL,  
  3.   BookName varchar(100) NOT NULL,  
  4.   BookType varchar(100) NOT NULL,  
  5.   BookVendor varchar(100) NOT NULL,  
  6.   BookDescription varchar(250),  
  7.   BookInStock int NOT NULL,  
  8.   Cost decimal(10,2) NOT NULL,  
  9.   PRIMARY KEY (BookCode)  
  10. ) ; 
Now, insert some records into the Books Table
  1. INSERT INTO Books(BookCode, BookName, BookType, BookVendor, BookDescription, BookInStock, Cost) VALUES    
  2. ('B10_001''Coding Fundamentals - C''Core Book''Min Lin Diecast'null, 7933, '48.81'),    
  3. ('B10_002''Coding Fundamentals - C++''Core Book''Classic Metal Creations'null, 7305, '98.58'),    
  4. ('B10_003''Coding Fundamentals - C#''Advanced Book''Mini Classics'null, 6625, '68.99'),    
  5. ('B10_004''Coding Fundamentals - PHP''E-Book''Start Diecast'null, 5582, '91.02'),    
  6. ('B10_005''Coding Fundamentals - Java''E-Book''City Art Classics'null, 3252, '85.68'),    
  7. ('B10_006''Coding Fundamentals - JavaScript''E-Book''Second Diecast'null, 6791, '103.42'),    
  8. ('B12_007''Coding Fundamentals - Angular''Advanced Book''Autoart Studio Design'null, 68, '95.34'),    
  9. ('B12_008''Coding Fundamentals - R''E-Book''Second Diecast'null, 3619, '95.59'),    
  10. ('B12_009''Coding Fundamentals - Python''Core Book''Welly Publications'null, 1579, '77.90'),    
  11. ('B12_010''Coding Fundamentals - HTML''Core Book''Art Galleries'null, 9997, '66.27'),    
  12. ('B12_011''Coding Fundamentals - CSS''Core Book''Welly Publications'null, 6906, '89.14'),    
  13. ('B12_012''Coding Fundamentals - CSS3''Core Book''Welly Publications'null, 9123, '75.16'),    
  14. ('B12_013''Coding Fundamentals - NodeJs''E-Book''Second Diecast'null, 1049, '83.05'),    
  15. ('B12_014''Coding Fundamentals - SQL SERVER''Advanced Book''Studio Art Model Productions'null, 5663, '31.92'),    
  16. ('B12_015''Coding Fundamentals - MySQL''E-Book''Designs Publications'null, 6125, '55.70'),    
  17. ('B12_016''Coding Fundamentals - Android''E-Book''Welly Diecast Publications'null, 7323, '58.73'),    
  18. ('B18_017''Coding Fundamentals - Machine Learning''E-Book''Studio Art Model Productions'null, 2613, '58.33'),    
  19. ('B18_018''AI''Digital Book''Mini Classics'null, 3975, '83.51'),    
  20. ('B18_019''Data Science''Digital Book''City Art Classics'null, 8693, '60.62'),    
  21. ('B18_020''Coding Advanced - C''E-Book''Studio Art Model Productions'null, 8635, '24.26'),    
  22. ('B18_021''Coding Advanced - C#''Core Book''Classic Creations Productions'null, 9042, '65.96'),    
  23. ('B18_022''Coding Advanced - C++''Advanced Book''Start Diecast Productions'null, 5330, '77.27'),    
  24. ('B18_023''Coding Advanced - JS''Core Book''Welly Publications'null, 2724, '86.70'),    
  25. ('B18_024''Coding Advanced - R''E-Book''Collectibles Productions'null, 8826, '53.90'),    
  26. ('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.

  1. SELECT MAX(Cost) AS Costly_Book  
  2. FROM books  
 

2) MAX() aggregate function can be used with GROUP BY clause to get the highest cost for every type of book

  1. SELECT BookType, max(Cost) AS Costly_Book  
  2. FROM books  
  3. GROUP BY BookType  
  4. 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 

  1. SELECT MIN(Cost) AS CheapCost_Book  
  2. FROM books 
 

2) MIN() aggregate function can be used with GROUP BY clause to get the low cost for every type of book

  1. SELECT BookType, MIN(Cost) AS CheapCost_Book  
  2. FROM books  
  3. GROUP BY BookType  
  4. ORDER BY CheapCost_Book ASC 
 

SUM() 

 
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.

  1. SELECT BookNumber, BookCode, SUM(bookissue * costeach) AS Total_price  
  2. FROM library  
  3. GROUP BY BookCode  
  4. ORDER BY Total_price DESC 
 

2) To calculate the sum of all the books that are in stock.

  1. SELECT BookType, SUM(BookInStock* Cost) AS TOTAL_COST  
  2. FROM Books  
  3. GROUP BY BookType 

AVG() Function

 
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’
  1. SELECT BookType, AVG(BookInStock * Cost) AS AverageCost  
  2. FROM Books  
  3. GROUP BY BookType  
  4. ORDER BY AverageCost DESC

COUNT()

 
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.
  1. SELECT BookType, COUNT(*)  
  2. FROM Books  
  3. GROUP BY BookType  
  4. ORDER BY BookType 
 

Conclusion

 
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!


Similar Articles