Find the top 10 users by total storage uploaded in a 30-day window

Quick Overview

Write a SQL query that ranks users by the total size of files they uploaded in a half-open 30-day window and returns the top 10, with totals rounded to two decimals. It tests joins, timestamp range filters, aggregation, and tie-breaking at a LIMIT cutoff.

Find the top 10 users by total storage uploaded in a 30-day window

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Find the top 10 users by total uploaded storage for files with `uploaded_at` in the half-open window `['2024-01-01', '2024-01-31')`. Return `user_id`, `username` and `total_storage_mb` (rounded to 2 decimals), ordered by `total_storage_mb` descending, then `user_id` ascending. ### Input Table(s) `users`, primary key `user_id`: | Column | Type | Meaning | |---|---|---| | user_id | INT | Unique user id | | username | VARCHAR(100) | Display name, NOT NULL | | email | VARCHAR(255) | Email address, NOT NULL | `files`, primary key `file_id`, with `user_id` a foreign key to `users.user_id`: | Column | Type | Meaning | |---|---|---| | file_id | INT | Unique file id | | user_id | INT | Owner of the file | | file_name | VARCHAR(255) | Name of the file, NOT NULL | | file_size_mb | DECIMAL(10,2) | File size in megabytes, NOT NULL, always positive | | uploaded_at | DATETIME (TIMESTAMP in PostgreSQL) | Date and time of the upload, NOT NULL | ### Output Contract - Columns, in this order: `user_id`, `username`, `total_storage_mb`. - `total_storage_mb` is the sum of `file_size_mb` over the user's files in the window, rounded to 2 decimal places. - At most 10 rows, one per user. - Sort by `total_storage_mb` descending, then by `user_id` ascending. The same order decides which users make the top 10. ### Example `users`: | user_id | username | email | |---|---|---| | 1 | ana | ana@example.com | | 2 | ben | ben@example.com | | 3 | cy | cy@example.com | | 4 | dee | dee@example.com | | 5 | eli | eli@example.com | `files`: | file_id | user_id | file_name | file_size_mb | uploaded_at | |---|---|---|---|---| | 201 | 1 | report.pdf | 120.40 | 2024-01-01 00:00:00 | | 202 | 1 | deck.pptx | 80.10 | 2024-01-30 23:59:59 | | 203 | 2 | movie.mkv | 200.50 | 2024-01-15 09:30:00 | | 204 | 2 | backup.tar | 900.00 | 2024-01-31 00:00:00 | | 205 | 3 | photos.zip | 350.00 | 2024-01-20 14:05:00 | | 206 | 4 | notes.txt | 0.75 | 2023-12-31 23:59:59 | | 207 | 5 | design.psd | 150.25 | 2024-01-05 08:00:00 | | 208 | 5 | assets.zip | 50.25 | 2024-01-25 17:45:00 | Expected result: | user_id | username | total_storage_mb | |---|---|---| | 3 | cy | 350.00 | | 1 | ana | 200.50 | | 2 | ben | 200.50 | | 5 | eli | 200.50 | Cy leads with 350.00. Ana's files at both edges of the window count, giving 120.40 + 80.10 = 200.50. Ben's backup at exactly 2024-01-31 00:00:00 is outside the half-open window, so he has 200.50 from one file, and eli has 150.25 + 50.25 = 200.50. The three-way tie is ordered by `user_id`. Dee's only file is from December, so she has no row. Only four users qualify, so all four fit under the limit of 10. ### Constraints and Clarifications - `users` has at most 10,000 rows and `files` at most 100,000 rows. - The window includes 2024-01-01 00:00:00 and every time before 2024-01-31 00:00:00, so the last day counted is January 30. - Users with no files in the window are not returned. If fewer than 10 users uploaded in the window, return fewer than 10 rows. - Return at most 10 rows even when several users tie for 10th place: the `user_id` tie-break decides who makes the cut. - `file_size_mb` is always positive, and every `user_id` in `files` references a valid user. - Each row of `files` is a separate file, so files that share a name all count toward the total. - Usernames are not guaranteed to be unique, so identify users by `user_id`. ```hint Window edges Check whether files uploaded at 2024-01-30 23:59:59 and at 2024-01-31 00:00:00 land inside your filter. ```

Overview: Write a SQL query that ranks users by the total size of files they uploaded in a half-open 30-day window and returns the top 10, with totals rounded to two decimals. It tests joins, timestamp range filters, aggregation, and tie-breaking at a LIMIT cutoff.

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

Find the top 10 users by total uploaded storage for files with uploaded_at in the half-open window ['2024-01-01', '2024-01-31'). Return user_id, username and total_storage_mb (rounded to 2 decimals), ordered by total_storage_mb descending, then user_id ascending.

Input Table(s)

users, primary key user_id:

ColumnTypeMeaning
user_idINTUnique user id
usernameVARCHAR(100)Display name, NOT NULL
emailVARCHAR(255)Email address, NOT NULL

files, primary key file_id, with user_id a foreign key to users.user_id:

ColumnTypeMeaning
file_idINTUnique file id
user_idINTOwner of the file
file_nameVARCHAR(255)Name of the file, NOT NULL
file_size_mbDECIMAL(10,2)File size in megabytes, NOT NULL, always positive
uploaded_atDATETIME (TIMESTAMP in PostgreSQL)Date and time of the upload, NOT NULL

Output Contract

  • Columns, in this order: user_id , username , total_storage_mb .
  • total_storage_mb is the sum of file_size_mb over the user's files in the window, rounded to 2 decimal places.
  • At most 10 rows, one per user.
  • Sort by total_storage_mb descending, then by user_id ascending. The same order decides which users make the top 10.

Example

users:

user_idusernameemail
1anaana@example.com
2benben@example.com
3cycy@example.com
4deedee@example.com
5elieli@example.com

files:

file_iduser_idfile_namefile_size_mbuploaded_at
2011report.pdf120.402024-01-01 00:00:00
2021deck.pptx80.102024-01-30 23:59:59
2032movie.mkv200.502024-01-15 09:30:00
2042backup.tar900.002024-01-31 00:00:00
2053photos.zip350.002024-01-20 14:05:00
2064notes.txt0.752023-12-31 23:59:59
2075design.psd150.252024-01-05 08:00:00
2085assets.zip50.252024-01-25 17:45:00

Expected result:

user_idusernametotal_storage_mb
3cy350.00
1ana200.50
2ben200.50
5eli200.50

Cy leads with 350.00. Ana's files at both edges of the window count, giving 120.40 + 80.10 = 200.50. Ben's backup at exactly 2024-01-31 00:00:00 is outside the half-open window, so he has 200.50 from one file, and eli has 150.25 + 50.25 = 200.50. The three-way tie is ordered by user_id. Dee's only file is from December, so she has no row. Only four users qualify, so all four fit under the limit of 10.

Constraints and Clarifications

  • users has at most 10,000 rows and files at most 100,000 rows.
  • The window includes 2024-01-01 00:00:00 and every time before 2024-01-31 00:00:00, so the last day counted is January 30.
  • Users with no files in the window are not returned. If fewer than 10 users uploaded in the window, return fewer than 10 rows.
  • Return at most 10 rows even when several users tie for 10th place: the user_id tie-break decides who makes the cut.
  • file_size_mb is always positive, and every user_id in files references a valid user.
  • Each row of files is a separate file, so files that share a name all count toward the total.
  • Usernames are not guaranteed to be unique, so identify users by user_id .