Returns
COUNT
of all rows of the
students
table
SELECT COUNT(*) AS students_count
FROM students
Returns
minimum
points from
students
table
SELECT MIN(points) AS minimum_points
FROM students
Returns
maximum
points from
students
table
SELECT MAX(points) AS maximum_points
FROM students
Returns
average
points from
students
table
SELECT AVG(points) AS average_points
FROM students
Returns
SUM
of all students points
SELECT SUM(points) AS 'sum of all points'
FROM students
Aggregate functions behaves differently when there is any number of additional columns with any of the aggregate functions in
SELECT
clause.
GROUP BY
clause is needed when there is any additional column from the table. In this example rows are grouped by
last_name
and the value that
SUM
function returns is aggregated. We get all the unique last names with its aggregated points
SELECT last_name, SUM(points) AS 'sum of all points'
FROM students
GROUP BY last_name
We can do the same for all the other aggregated functions Complete list of aggregate functions: https://dev.mysql.com/doc/refman/5.6/en/aggregate-functions.html
by Valeri Tandilashvili
4 years ago
MySQL
1
Pro tip: use ```triple backticks around text``` to write in code fences