Enrollment No.: Experiment Date: Experiment No.- 5 Aim: To write a query for aggregate functions like AVG, COUNT, SUM, MAX, MIN. 1. Aggregate Function in Mysql The data that you need is not always directly stored in the tables. However, you can get it by performing the calculations of the stored data when you select it. By definition, an aggregate function performs a calculation on a set of values and returns a single value. MySQL provides many aggregate functions that include AVG , COUNT , SUM , MIN , MAX , etc. An aggregate function ignores NULL values when it performs calculation except for the COUNT function. 2. AVG function The AVG function calculates the average value of a set of values. It ignores NULL values in the calculation. AVG function is use to calculate the average buy price of all products in the products table by using the following query: * SELECT AVG(buyPrice) average_buy_price FROM products Y ou can add a WHERE clause to the SELECT statement to calculate the average value of a subset of values. For example, to calculate the average buy price of products whose product line is Classic Cars , you use the following query * SELECT AVG(buyprice) 'Average Classic Cars Price' FROM products Name of Student: Submission Date: Enrollment No.: Experiment Date: WHERE productline = 'Classic Cars'; You can use AVG()function to calculate the average of unique buy prices by adding the DISTINCT operator as follows * SELECT FORMAT(AVG(DISTINCT buyprice), 2) FROM products; 3. COUNT function The COUNT function returns the number of rows in a table. The COUNT function allows you to count all rows in a table or just the rows that match a specified condition. The return type of the COUNT function is BIGINT. The COUNT function will return zero if there was no matching row found. There are several forms of the COUNT function: COUNT(*), COUNT(expression) and COUNT (DISTINCT expression). 3.1 MySQL COUNT(*) The COUNT(*)function returns the number of rows in a result set returned by a SELECT statement. The COUNT(*) function counts rows that contain non-NULL and NULL values. create a new table named demos and insert some sample data for the emonstration. CREATE TABLE demos( id int auto_increment primary key,val int ); Insert following data in table demos To count all rows in the demos table, you use the COUNT(*) function as follows: SELECT COUNT(*) FROM demos; You can add a WHERE clause to specify a condition to count e.g., to count only rows whose values in the val column are 2, you use the following query: SELECT COUNT(*) FROM demos WHERE val = 2; Name of Student: Submission Date: Enrollment No.: Experiment Date: 3.2 MySQL COUNT(expression) The COUNT(expression) returns the number of rows that do not contain NULL values. 3.3 MySQL COUNT(DISTINCT expression) The COUNT(DISTINCT expression) returns the number of unique rows that do not contain NULLvalues. To count unique rows in the demos table, you add the DISTINCT operator to the COUNT function as the following query: SELECT COUNT(DISTINCT val) FROM demos; 4. SUM function The SUM() function is an aggregate function that allows you to calculate the sum of a set of values or an expression. How the SUM() function works: If you use the SUM() function in a SELECT statement that returns no matching row, the SUM()function returns NULL, not zero. The DISTINCT operator allows you to calculate distinct values in the set. The SUM() function ignores the NULL values in the calculation. * CREATE TABLE `orderdetails` ( `orderNumber` int(11) NOT NULL, `productCode` varchar(15) NOT NULL, `quantityOrdered` int(11) NOT NULL, `priceEach` decimal(10,2) NOT NULL, `orderLineNumber` smallint(6) NOT NULL) ; *Insert into `orderdetails values (10100,'S18_1749',30,'136.00',3), (10100,'S18_2248',50,'55.09',2), (10100,'S18_4409',22,'75.46',4), (10100,'S24_3969',49,'35.29',1), (10101,'S18_2325',25,'108.06',4),; You can calculate the total amount of the order number 10100 by using the SUM() function as the following query: SELECT (SUM(quantityOrdered * priceEach),total FROM orderdetails WHERE orderNumber = 10100; Name of Student: Submission Date: Enrollment No.: Experiment Date: 5. MySQL MAX function The MySQL MAX function returns the maximum value in a set of values. The MAX function is handy in many queries such as finding the greatest number, the most expensive product, and the largest payment from customers. *CREATE TABLE `payments` ( `customerNumber` int(11) NOT NULL, `checkNumber` varchar(50) NOT NULL, `paymentDate` date NOT NULL, `amount` decimal(10,2) NOT NULL); *Insert into `payments values (103,'HQ336336','2004-10-19','6066.78'), (103,'JM555205','2003-06-05','14571.44'), (103,'OM314933','2004-12-18','1676.14'), (112,'BO864823','2004-12-17','14191.12'), (112,'HQ55022','2003-06-06','32641.98'), (112,'ND748579','2004-08-20','33347.88'); To get the largest payment in the payments table, you use the following query: SELECT MAX(amount) FROM payments; To get not only the largest payment’s amount but also other payment’s information such as customer number, check number and payment date, you use the MAX function in a subquery as follows: SELECT * FROM payments WHERE amount = (SELECT MAX(amount) FROM payments); 6. MySQL MIN function The MIN function returns the minimum value in a set of values. The MIN function is very useful in some scenarios such as finding the smallest number, selecting the least expensive product, getting the lowest credit limit, etc. To get the cheapest product in the products table, which is the product that has the lowest buy price, you use the following query: SELECT MIN(buyPrice) FROM products; To select not only the price but also other product’s information such as product code and product name, you use the MIN function in a subquery as follows: SELECT productCode, productName, buyPrice FROM products WHERE buyPrice = (SELECT MIN(buyPrice FROM products); Name of Student: Submission Date: