7
7
AIM: Study and implement aggregation functions like min, max, avg, sum, count.
THEORY:
Aggregate functions allow us to easily produce summarized data from our database.
COUNT
The COUNT function returns the total number of values in the specified field. It works on both
numeric and non-numeric data types. All aggregate functions by default exclude nulls values
before working on the data.
COUNT (*) is a special implementation of the COUNT function that returns the count of all the
rows in a specified table. COUNT (*) also considers Nulls and duplicates.
Insert data
DISTINCT Keyword
The DISTINCT keyword that allows us to omit duplicates from our results. This is achieved by
grouping similar values together.
Syntax / SELECT DISTINCT col_name SELECT DISTINCT customer_id FROM
Example FROM table_name; Customers;
MIN function
The MIN function returns the smallest value in the specified table field.
As an example, let’s suppose we want to know the year in which the oldest movie in our library
was released, we can use MySQL’s MIN function to get the desired information.
MAX function
Just as the name suggests, the MAX function is the opposite of the MIN function. It returns the
largest value from the specified table field.
Let’s assume we want to get the year that the latest movie in our database was released. We can
easily use the MAX function to achieve that.
SUM function
Suppose we want a report that gives total amount of payments made so far. We can use the
MySQL SUM function which returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result returned.