RianAcademy
Search
⌘
K
Premium

Aggregate Functions

In the article about grouping, we discussed that when using the GROUP BY operator, we can use aggregate functions. Let's talk more about them 🐳.

General structure of a query with an aggregate function

MySQL 8.4
SELECT { literal | aggregate_function | grouping_field } [, ...]
FROM table_name
GROUP BY grouping_field [, ...];
  • [ ]optional part
  • { a | b }one of the options
  • ...can be repeated
literal

A fixed value such as 'text' or 1. It is the same for every group.

aggregate_function

A function that computes one value from all rows of a group, for example AVG(price).

grouping_field

The column that splits rows into groups: rows with the same value end up in one group. You can list several columns separated by commas.

table_name
The table whose rows are grouped.

For example, a query using the AVG aggregate function might look like this:

MySQL 8.4
SELECT home_type, AVG(price) as avg_price FROM Rooms
GROUP BY home_type
home_typeavg_price
Private room89.4286
Entire home/apt148.6667
Shared room40

Description of aggregate functions

FunctionDescription
SUM(table_field)Returns the sum of the values
AVG(table_field)Returns the average value
COUNT(table_field)Returns the number of records
MIN(table_field)Returns the minimum value
MAX(table_field)Returns the maximum value

Examples

Users
id
INT
name
VARCHAR
email
VARCHAR
email_verified_at
DATETIME
password
VARCHAR
phone_number
VARCHAR
Reservations
id
INT
user_id
INT
room_id
INT
start_date
DATETIME
end_date
DATETIME
price
INT
total
INT
Rooms
id
INT
home_type
VARCHAR
address
VARCHAR
has_tv
BOOLEAN
has_internet
BOOLEAN
has_kitchen
BOOLEAN
has_air_con
BOOLEAN
price
INT
owner_id
INT
latitude
FLOAT
longitude
FLOAT
Reviews
id
INT
reservation_id
INT
rating
INT
  • Find the number of each type of home and sort the resulting list in descending order:

    MySQL 8.4
    SELECT home_type, COUNT(*) as amount FROM Rooms
    GROUP BY home_type
    ORDER BY amount DESC
    
    home_typeamount
    Private room28
    Entire home/apt21
    Shared room1
  • For each room, find the latest end date of reservations (the end_date field)

    MySQL 8.4
    SELECT room_id, MAX(end_date) AS last_end_date FROM Reservations
    GROUP BY room_id
    
    room_idlast_end_date
    12019-02-04T12:00:00.000Z
    22020-03-23T09:00:00.000Z
    132020-04-21T10:00:00.000Z
    162019-06-24T10:00:00.000Z
    212020-02-29T10:00:00.000Z
    192020-05-02T10:00:00.000Z
    82020-01-21T12:00:00.000Z
    72019-09-17T10:00:00.000Z
    52020-05-15T10:00:00.000Z
    502019-11-25T11:00:00.000Z
    492020-06-11T10:00:00.000Z
    482019-11-10T10:00:00.000Z
    322020-01-18T13:00:00.000Z
    172019-11-05T09:00:00.000Z
    252020-04-22T09:00:00.000Z
    142020-02-12T10:00:00.000Z
    392019-12-09T10:00:00.000Z
    382020-03-23T10:00:00.000Z
Tasks for self-testing
0 / 4
1. Grouping and sorting

Count the number of students in each class and also sort them in descending order of the number of students. You can get the student's belonging to a specific class from the Student_in_class. table. As a result, display the class identifier (class field) and the number of students in this class.
Use the count alias to display the number of students.
Class
id
INT
name
VARCHAR
Schedule
id
INT
date
DATE
class
INT
number_pair
INT
teacher
INT
subject
INT
classroom
INT
Student
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
birthday
DATE
address
VARCHAR
Student_in_class
id
INT
class
INT
student
INT
Subject
id
INT
name
VARCHAR
Teacher
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
Timepair
id
INT
start_pair
TIME
end_pair
TIME
SELECT class, _____ AS count FROM Student_in_class GROUP BY _____
    ORDER BY _____
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Class
Schedule
Student
Student_in_class
Subject
Teacher
Timepair

Run a query to see the result, or open a table above