The datasets table holds one row per dataset with its category, its size in megabytes, an active flag and the time it was last updated.
Aggregate the active datasets (is_active = 1) per category, reporting dataset_count, total_size_mb, avg_size_mb (rounded to 2 decimals) and latest_update (the maximum updated_at). Only include categories with at least 2 active datasets. Order by dataset_count descending, then category ascending.
Input Table(s)
datasets
| Column | Type | Meaning |
|---|
| dataset_id | INT | Dataset identifier |
| name | VARCHAR(100) | Dataset name |
| category | VARCHAR(50) | Category the dataset belongs to |
| size_mb | DECIMAL(10,2) | Size in megabytes |
| is_active | SMALLINT | 1 if the dataset is active, 0 otherwise |
| updated_at | TIMESTAMP | When the dataset was last updated |
Primary key: dataset_id.
Output Contract
One row per qualifying category, with these columns in this order:
| Column | Definition |
|---|
| category | The category |
| dataset_count | Number of active datasets in the category |
| total_size_mb | Sum of size_mb over the active datasets (no rounding needed, sizes have 2 decimals) |
| avg_size_mb | Average size_mb over the active datasets, rounded to 2 decimal places |
| latest_update | Latest updated_at among the active datasets |
Order by dataset_count descending, then category ascending. Each category appears once, so this order is unique.
Example
datasets
| dataset_id | name | category | size_mb | is_active | updated_at |
|---|
| 1 | squad-mini | nlp | 120.50 | 1 | 2024-03-01 10:00:00 |
| 2 | imdb-reviews | nlp | 80.30 | 1 | 2024-05-12 08:30:00 |
| 3 | wiki-dump | nlp | 900.00 | 0 | 2024-06-01 00:00:00 |
| 4 | cifar-small | vision | 150.00 | 1 | 2024-04-20 14:15:00 |
| 5 | coco-subset | vision | 300.10 | 1 | 2024-02-11 09:00:00 |
| 6 | librispeech-clean | audio | 500.00 | 1 | 2024-01-05 12:00:00 |
| 7 | common-voice-tiny | audio | 45.00 | 0 | 2024-07-07 07:07:00 |
| 8 | tabular-housing | tabular | 2.40 | 1 | 2024-03-15 16:45:00 |
| 9 | credit-default | tabular | 3.10 | 1 | 2024-03-18 11:20:00 |
| 10 | titanic | tabular | 0.06 | 1 | 2024-01-30 09:10:00 |
Expected result:
| category | dataset_count | total_size_mb | avg_size_mb | latest_update |
|---|
| tabular | 3 | 5.56 | 1.85 | 2024-03-18 11:20:00 |
| nlp | 2 | 200.80 | 100.40 | 2024-05-12 08:30:00 |
| vision | 2 | 450.10 | 225.05 | 2024-04-20 14:15:00 |
The inactive wiki-dump row does not count toward nlp, so its later date and large size are ignored. audio has only one active dataset and is dropped. nlp and vision tie on count and are ordered by category, so nlp comes first.
Constraints and Clarifications
-
The table has 1 to 100,000 rows.
-
size_mb
is between 0 and 1,000,000.
-
is_active
is 0 or 1. Inactive datasets are excluded from every output column, including
latest_update
.
-
category
is a non-empty string of at most 50 characters, compared exactly as stored.
-
A category with fewer than 2 active datasets is left out, no matter how many inactive datasets it has.
-
dataset_id
is unique, so every row is a separate dataset. Two rows with the same
name
count twice.
-
The constraints give every row a
size_mb
value. If a NULL did appear,
SUM
and
AVG
would skip it while
COUNT(*)
would still count the row.