Aggregate active datasets per category with count, total and average size, and latest update

Quick Overview

Write a PostgreSQL query that groups active datasets by category and reports the count, total size, rounded average size and most recent update, keeping only categories with at least two active datasets. It tests row filters versus group filters, aggregate functions and deterministic ordering.

Aggregate active datasets per category with count, total and average size, and latest update

Company: Hugging Face

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Two kinds of filter One condition applies to individual rows and the other applies to whole categories. Decide which clause each one belongs in. ``` ### 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.

Overview: Write a PostgreSQL query that groups active datasets by category and reports the count, total size, rounded average size and most recent update, keeping only categories with at least two active datasets. It tests row filters versus group filters, aggregate functions and deterministic ordering.

Hugging Face logo
Hugging Face
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
dataset_idINTDataset identifier
nameVARCHAR(100)Dataset name
categoryVARCHAR(50)Category the dataset belongs to
size_mbDECIMAL(10,2)Size in megabytes
is_activeSMALLINT1 if the dataset is active, 0 otherwise
updated_atTIMESTAMPWhen the dataset was last updated

Primary key: dataset_id.

Output Contract

One row per qualifying category, with these columns in this order:

ColumnDefinition
categoryThe category
dataset_countNumber of active datasets in the category
total_size_mbSum of size_mb over the active datasets (no rounding needed, sizes have 2 decimals)
avg_size_mbAverage size_mb over the active datasets, rounded to 2 decimal places
latest_updateLatest 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_idnamecategorysize_mbis_activeupdated_at
1squad-mininlp120.5012024-03-01 10:00:00
2imdb-reviewsnlp80.3012024-05-12 08:30:00
3wiki-dumpnlp900.0002024-06-01 00:00:00
4cifar-smallvision150.0012024-04-20 14:15:00
5coco-subsetvision300.1012024-02-11 09:00:00
6librispeech-cleanaudio500.0012024-01-05 12:00:00
7common-voice-tinyaudio45.0002024-07-07 07:07:00
8tabular-housingtabular2.4012024-03-15 16:45:00
9credit-defaulttabular3.1012024-03-18 11:20:00
10titanictabular0.0612024-01-30 09:10:00

Expected result:

categorydataset_counttotal_size_mbavg_size_mblatest_update
tabular35.561.852024-03-18 11:20:00
nlp2200.80100.402024-05-12 08:30:00
vision2450.10225.052024-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.