RienSQL
LESSON 19 / 62 · Aggregate functions

Aggregate functions

Aggregate functions in SQL queries, syntax, and examples

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 🐳.

An aggregate function is a function that performs a calculation on a set of values and returns a single value.

General structure of a query with an aggregate function

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

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

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

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:

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

Aggregate functions apply to values that are not NULL. The exception is the COUNT(*) function.

Examples

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)

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

Python Academy

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Grouping 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.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Aggregation functions MIN and MAX
For each existing status, find the oldest person (use birthday field). Display the status and date of birth.
Use the birthday alias to display the date of birth.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3Aggregation function AVG
Get the average flight time for each aircraft model. Output the plane field and the average flight time in seconds.
Use the time alias to display the time.
To get the time difference in seconds between two dates use:
For MySQL: TIMESTAMPDIFF(second, time_out, time_in)
For PostgreSQL: EXTRACT(EPOCH FROM (time_in - time_out))
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 4SELECTing using multiple aggregation functions
Output the room ID (field room_id), the average cost for one day of rent (the price field, use the avg_price alias for display), and the number of reservations for this room (use the count alias). Sort the result in descending order, first by the number of reservations, and then by the average cost.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.