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.
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.
Table `datasets`:
- `dataset_id` INTEGER: dataset identifier, unique per row
- `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
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.
Return one row per qualifying category with these columns in this order:
- `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.
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.
Tables
datasets(dataset_id INTEGER, name VARCHAR(100), category VARCHAR(50), size_mb DECIMAL(10,2), is_active SMALLINT, updated_at TIMESTAMP)
Hints
- One condition applies to individual rows and the other applies to whole categories. Decide which clause each one belongs in.
- Every output column is computed over active datasets only, so an inactive row with a later `updated_at` or a larger `size_mb` should not change any value in its category.