SQLite Aggregate Functions
SQLite Aggregate Functions
SQLite Aggregate Functions
in this tutorial, you will learn about the SQLite aggregate functions to find the maximum,
minimum, average, sum, and count of a set of values.
Aggregate functions operate on a set of rows and return a single result.
SQLite provides the following aggregate functions:
We will use the tracks table from the sample database for the demonstration:
In this example:
• First, the GROUP BY clause divides the tracks by album id into groups.
• Then, the AVG() function applies to each group that has the same album id to calculate
the average length of tracks.
SELECT
COUNT(*)
FROM
tracks;
To find the albums and their corresponding track count, you use the following statement:
SELECT
AlbumId,
COUNT(TrackId) track_count
FROM
tracks
GROUP BY
AlbumId
ORDER BY
track_count DESC;
SELECT
AlbumId,
SUM(Milliseconds) / 60000 Minutes
FROM
tracks
GROUP BY
AlbumId;
Select trackid,
name,max(milliseconds)
from tracks;
Similarly, the following statement finds the track whose length is shortest by using
the MIN() function:
SELECT
TrackId,
Name,
MIN(Milliseconds)
FROM
tracks
Tasks:
Find the total unit price of all the tracks.
Find the number of tracks in each media type.
Find the number of tracks in each genre.
Find the total numbers of distinct countries from customers table.
Find the average time of each album in seconds.
Find the youngest employee in the company.
Find the total quantity of invoice items.
Find the track with maximum bytes.