LESSON 19 / 62 · Aggregate functionsAggregate 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_type | avg_price |
|---|---|
| Private room | 89.4286 |
| Entire home/apt | 148.6667 |
| Shared room | 40 |
Description of aggregate functions
| Function | Description |
|---|---|
| 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_type | amount |
|---|---|
| Private room | 28 |
| Entire home/apt | 21 |
| Shared room | 1 |
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_id | last_end_date |
|---|---|
| 1 | 2019-02-04T12:00:00.000Z |
| 2 | 2020-03-23T09:00:00.000Z |
| 13 | 2020-04-21T10:00:00.000Z |
| 16 | 2019-06-24T10:00:00.000Z |
| 21 | 2020-02-29T10:00:00.000Z |
| 19 | 2020-05-02T10:00:00.000Z |
| 8 | 2020-01-21T12:00:00.000Z |
| 7 | 2019-09-17T10:00:00.000Z |
| 5 | 2020-05-15T10:00:00.000Z |
| 50 | 2019-11-25T11:00:00.000Z |
| 49 | 2020-06-11T10:00:00.000Z |
| 48 | 2019-11-10T10:00:00.000Z |
| 32 | 2020-01-18T13:00:00.000Z |
| 17 | 2019-11-05T09:00:00.000Z |
| 25 | 2020-04-22T09:00:00.000Z |
| 14 | 2020-02-12T10:00:00.000Z |
| 39 | 2019-12-09T10:00:00.000Z |
| 38 | 2020-03-23T10:00:00.000Z |
Python Academy
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Use the count alias to display the number of students.
Write a query and run it to see the results.
Use the birthday alias to display the date of birth.
Write a query and run it to see the results.
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))
Write a query and run it to see the results.
Write a query and run it to see the results.