Quick Overview

Write a SQL query that totals each user's uploads over a 30-day window, converts megabytes to gigabytes, and ranks users by the rounded total. It tests joins, half-open date filters, aggregation, unit conversion that avoids integer division, and ordering on a rounded value.

Rank users by total gigabytes uploaded in a 30-day window

Company: Box

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For uploads with `upload_date` in `['2024-01-01', '2024-01-31')` (start inclusive, end exclusive), return each user's `user_id`, `username`, and total uploaded data in GB as `total_gb` (rounded to 2 decimals). Include only users with at least one upload in that period. Order by `total_gb` descending, then `user_id` ascending. ### Input Table(s) `users`, primary key `user_id`: | Column | Type | Meaning | |---|---|---| | user_id | INT | Unique user id | | username | VARCHAR | Display name | `uploads`, primary key `upload_id`, with `user_id` referring to `users.user_id`: | Column | Type | Meaning | |---|---|---| | upload_id | INT | Unique upload id | | user_id | INT | User who made the upload | | file_size_mb | DECIMAL | Size of the upload in megabytes, always positive | | upload_date | DATE | Calendar date of the upload | ### Output Contract - Columns, in this order: `user_id`, `username`, `total_gb`. - `total_gb` is the user's summed `file_size_mb` in the window divided by 1024 (1 GB = 1024 MB), rounded to 2 decimal places. - One row per user with at least one upload in the window. - Sort by the rounded `total_gb` descending, then by `user_id` ascending. ### Example `users`: | user_id | username | |---|---| | 1 | ana | | 2 | ben | | 3 | cy | | 4 | dee | `uploads`: | upload_id | user_id | file_size_mb | upload_date | |---|---|---|---| | 301 | 1 | 1024.00 | 2024-01-01 | | 302 | 2 | 1000.00 | 2024-01-10 | | 303 | 2 | 27.00 | 2024-01-30 | | 304 | 3 | 2048.00 | 2024-01-31 | | 305 | 3 | 512.00 | 2024-01-15 | | 306 | 4 | 300.00 | 2023-12-31 | Expected result: | user_id | username | total_gb | |---|---|---| | 1 | ana | 1.00 | | 2 | ben | 1.00 | | 3 | cy | 0.50 | Ana has 1024.00 MB, exactly 1.00 GB. Ben has 1000.00 + 27.00 = 1027.00 MB, about 1.0029 GB, which also rounds to 1.00, so the tie on `total_gb` is broken by `user_id` and ana comes first. Cy's 2024-01-31 upload is outside the window, leaving 512.00 MB, or 0.50 GB. Dee's only upload is from December, so dee has no row. ### Constraints and Clarifications - `users` has at most 10,000 rows and `uploads` at most 100,000 rows. - The window covers 2024-01-01 through 2024-01-30. Uploads dated 2024-01-31 are excluded. - Convert the user's total, then round. Rounding each upload before summing gives different results. - Ordering uses the rounded `total_gb`, so users whose totals round to the same value are ordered by `user_id`, as in the example. - Users with no uploads in the window do not appear. - Each `upload_id` is unique and every upload row counts toward its user's total. - An upload whose `user_id` has no matching row in `users` is not counted. ```hint Units Decide at which step the megabyte-to-gigabyte conversion and the rounding happen, and make sure the division keeps its decimal places. ```

Overview: Write a SQL query that totals each user's uploads over a 30-day window, converts megabytes to gigabytes, and ranks users by the rounded total. It tests joins, half-open date filters, aggregation, unit conversion that avoids integer division, and ordering on a rounded value.

Using the `users` and `uploads` tables, consider only uploads with `upload_date` in `['2024-01-01', '2024-01-31')` (start inclusive, end exclusive). For each user with at least one upload in that period, return `user_id`, `username`, and the total uploaded data in GB as `total_gb`, rounded to 2 decimal places. Order by `total_gb` descending, then `user_id` ascending. ### Input tables `users`, primary key `user_id`: | Column | Type | Meaning | |---|---|---| | user_id | INTEGER | Unique user id | | username | VARCHAR(50) | Display name | `uploads`, primary key `upload_id`, with `user_id` referring to `users.user_id`: | Column | Type | Meaning | |---|---|---| | upload_id | INTEGER | Unique upload id | | user_id | INTEGER | User who made the upload | | file_size_mb | DECIMAL(12,2) | Size of the upload in megabytes, always positive | | upload_date | DATE | Calendar date of the upload | ### Output - Columns, in this order: `user_id`, `username`, `total_gb`. - `total_gb` is the user's summed `file_size_mb` in the window divided by 1024 (1 GB = 1024 MB), rounded to 2 decimal places. - One row per user with at least one upload in the window. - Sort by the rounded `total_gb` descending, then by `user_id` ascending. ### Clarifications - `users` has at most 10,000 rows and `uploads` at most 100,000 rows. - The window covers 2024-01-01 through 2024-01-30. Uploads dated 2024-01-31 are excluded. - Convert the user's total, then round. Rounding each upload before summing gives different results. - Ordering uses the rounded `total_gb`, so users whose totals round to the same value are ordered by `user_id`. For example, a user with 1024.00 MB and a user with 1027.00 MB both get `total_gb` 1.00, and the one with the smaller `user_id` comes first. - Users with no uploads in the window do not appear. - Each `upload_id` is unique and every upload row counts toward its user's total. - An upload whose `user_id` has no matching row in `users` is not counted.

Tables

users(user_id INTEGER, username VARCHAR(50))

uploads(upload_id INTEGER, user_id INTEGER, file_size_mb DECIMAL(12,2), upload_date DATE)

Hints

  1. Decide at which step the megabyte-to-gigabyte conversion and the rounding happen, and make sure the division keeps its decimal places.
  2. The window includes 2024-01-01 and stops before 2024-01-31, so the start date is included and the end date is excluded.
BoxData ScientisteasyData Manipulation (SQL/Python)

Total January Upload Volume per User in GB

Using the users and uploads tables, consider only uploads with upload_date in ['2024-01-01', '2024-01-31') (start inclusive, end exclusive). For each user with at least one upload in that period, return user_id, username, and the total uploaded data in GB as total_gb, rounded to 2 decimal places. Order by total_gb descending, then user_id ascending.

Input tables

users, primary key user_id:

ColumnTypeMeaning
user_idINTEGERUnique user id
usernameVARCHAR(50)Display name

uploads, primary key upload_id, with user_id referring to users.user_id:

ColumnTypeMeaning
upload_idINTEGERUnique upload id
user_idINTEGERUser who made the upload
file_size_mbDECIMAL(12,2)Size of the upload in megabytes, always positive
upload_dateDATECalendar date of the upload

Output

  • Columns, in this order: user_id, username, total_gb.
  • total_gb is the user's summed file_size_mb in the window divided by 1024 (1 GB = 1024 MB), rounded to 2 decimal places.
  • One row per user with at least one upload in the window.
  • Sort by the rounded total_gb descending, then by user_id ascending.

Clarifications

  • users has at most 10,000 rows and uploads at most 100,000 rows.
  • The window covers 2024-01-01 through 2024-01-30. Uploads dated 2024-01-31 are excluded.
  • Convert the user's total, then round. Rounding each upload before summing gives different results.
  • Ordering uses the rounded total_gb, so users whose totals round to the same value are ordered by user_id. For example, a user with 1024.00 MB and a user with 1027.00 MB both get total_gb 1.00, and the one with the smaller user_id comes first.
  • Users with no uploads in the window do not appear.
  • Each upload_id is unique and every upload row counts toward its user's total.
  • An upload whose user_id has no matching row in users is not counted.

Expected Output

Tables

usersuploads
users
ColumnType
user_idINTEGER
usernameVARCHAR(50)
uploads
ColumnType
upload_idINTEGER
user_idINTEGER
file_size_mbDECIMAL(12,2)
upload_dateDATE

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions