COUNT counts rows, but sometimes we want to compute something from the numeric values in a column. Four more functions serve this purpose.
SUM adds together all the values in a column:
1SELECT SUM(cena) FROM ksiazki;This means: "How much are all the books worth together?". The Library will add up the prices of all the scrolls.
AVG (from average) computes the typical value:
1SELECT AVG(cena) FROM ksiazki;This means: "How much does one book cost on average?". MySQL adds all the prices and divides by the number of books.
MIN returns the smallest value, and MAX the largest:
1SELECT MIN(cena) FROM ksiazki;
2SELECT MAX(cena) FROM ksiazki;The first query finds the cheapest book (its price), the second - the most expensive. MIN and MAX also work on dates (earliest and latest) and on text (alphabetically first and last).
You can ask for several summaries at once, listing them separated by commas:
1SELECT COUNT(*), AVG(cena), MIN(cena), MAX(cena) FROM ksiazki;One query, and the Library answers: how many books we have, what the average price is, the cheapest and the most expensive. Four numbers at a single glance!