The aggregate functions are functions that allow you to perform operations on a result set, returning a single aggregate value. They are often used in conjunction with the GROUP BY clause.
COUNT
The COUNT function, like the rest of aggregate functions, is always placed in the SELECT before the FROM. It is used to count the number of records. If it does not carry any condition, it will return the total number of records.
| totalCustomers |
|---|
| 7 |
The result of the statement tells us that the Customers table currently consists of a total of 7 records.
SELECT. In addition, they are not limited to a single function per statement, and several can be applied in the same query.Next, we will see another simple example with the Orders table:
| totalAlexa |
|---|
| 2 |
The output tells us that the Orders table contains two Alexa ECHO devices.
alias for the definition of the columns that store said values of the result set.MAX
The MAX function returns the maximum value of the specified field:
| maxPrice |
|---|
| 2449,5 |
This query tells us that our most expensive product in the Orders table has a value of: 2449.50 euros.
MIN
The MIN function returns the minimum value of the specified field:
| minPrice |
|---|
| 39,99 |
This query tells us that our cheapest product in the Orders table has a value of: 39.99 euros.
SUM
The SUM function returns the total value of the sum of a column. This function is only applicable to numeric fields, and NULL values are ignored.
| Profit |
|---|
| 17251,6 |
Through this query we quickly access the final result of the sum of all the total prices of the Orders table.
AVG
The AVG function returns the average value of the specified field. This function is only applicable to numeric fields and NULL values will be ignored.
| averagePrice |
|---|
| 610,73875 |
This query tells us the value of the average price from the Orders table.
GROUP BY
The GROUP BY clause is used to group the results according to the selected column, returning a row of results for each group of said column.
GROUP BY is often used with aggregate functions.
As for the syntax, the GROUP BY clause is placed at the end of the statement, after WHERE but before ORDER BY, in the case that both clauses are applied.
First, we run the following SELECT example without adding a GROUP BY clause:
| customerId | name | lastName |
|---|---|---|
| 1 | Janice | Haynes |
| 2 | Mavin | Pettitt |
| 2 | Mavin | Pettitt |
| 3 | Peter | Davis |
| 4 | Helen | Ward |
| 4 | Helen | Ward |
| 5 | Kimberly | Lee |
When establishing an INNER JOIN relationship between the Customers and Orders table, this SELECT allows us to see which Customers have placed an order, therefore, the customerId is present in the Orders table as many times as orders were placed.
Now, we will run the same query with GROUP BY to see the difference in the execution result:
| customerId | name | lastName |
|---|---|---|
| 1 | Janice | Haynes |
| 2 | Mavin | Pettitt |
| 3 | Peter | Davis |
| 4 | Helen | Ward |
| 5 | Kimberly | Lee |
As we can see, now the records of the same value have been grouped, eliminating duplicity in the query.
As mentioned above, GROUP BY is often used in conjunction with aggregate functions to extend the information returned in the query:
| customerId | name | lastName | numberOrders | customerTotalSpending |
|---|---|---|---|---|
| 1 | Janice | Haynes | 1 | 2449,5 |
| 2 | Mavin | Pettitt | 2 | 3199,78 |
| 3 | Peter | Davis | 1 | 4304,25 |
| 4 | Helen | Ward | 2 | 4999 |
| 5 | Kimberly | Lee | 1 | 899,97 |
Thanks to the aggregated functions, we can check the number of orders made by each client and the Total amount spent according to the number of orders made.