Average Stream Duration per User for One Topic in the Last 24 Hours
Company: Confluent
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write an optimized SQL query that returns each user's average stream duration, in seconds, for the topic `'gaming'` over the last 24 hours, relative to the reference time `'2024-01-15 00:00:00'`. Only completed streams count: a stream qualifies when its `start_time` is at or after `'2024-01-14 00:00:00'` and its `end_time` is not NULL.
### Input Table(s)
Table `users` (primary key `user_id`):
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier |
| username | VARCHAR(100) | Display name, NOT NULL |
Table `streams` (primary key `stream_id`; `user_id` is a foreign key to `users.user_id`):
| Column | Type | Meaning |
|---|---|---|
| stream_id | INT | Stream identifier |
| user_id | INT | The user who ran the stream |
| topic | VARCHAR(100) | Stream topic, NOT NULL |
| start_time | TIMESTAMP | When the stream began, NOT NULL |
| end_time | TIMESTAMP | When the stream ended; NULL while the stream is still ongoing |
### Output Contract
- Columns, in this order: `user_id`, `username`, `avg_duration_seconds`.
- One row per user who has at least one qualifying stream.
- `avg_duration_seconds` is the mean of the per-stream durations `end_time - start_time`, measured in seconds, with every qualifying stream weighted equally. Round it to 2 decimal places.
- Order by `avg_duration_seconds` descending (the rounded value), then by `user_id` ascending.
### Example
`users`
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | carol |
| 4 | dave |
`streams`
| stream_id | user_id | topic | start_time | end_time |
|---|---|---|---|---|
| 1 | 1 | gaming | 2024-01-14 10:00:00 | 2024-01-14 11:00:00 |
| 2 | 1 | gaming | 2024-01-14 20:00:00 | 2024-01-14 20:30:00 |
| 3 | 1 | gaming | 2024-01-14 23:00:00 | 2024-01-14 23:01:40 |
| 4 | 2 | gaming | 2024-01-14 08:00:00 | 2024-01-14 08:45:00 |
| 5 | 2 | gaming | 2024-01-14 22:00:00 | NULL |
| 6 | 3 | gaming | 2024-01-13 23:00:00 | 2024-01-14 01:00:00 |
| 7 | 3 | music | 2024-01-14 12:00:00 | 2024-01-14 13:00:00 |
| 8 | 4 | gaming | 2024-01-14 15:00:00 | 2024-01-14 15:45:00 |
Expected result:
| user_id | username | avg_duration_seconds |
|---|---|---|
| 2 | bob | 2700.00 |
| 4 | dave | 2700.00 |
| 1 | alice | 1833.33 |
Alice has three qualifying streams of 3600, 1800 and 100 seconds, so her average is 5500 / 3 = 1833.33. Bob's ongoing stream 5 is skipped, which leaves one 2700-second stream, and Dave also has one 2700-second stream, so the tie goes to the lower `user_id`. Carol does not appear: stream 6 started before the window, even though it ended inside it, and stream 7 is about music.
### Constraints and Clarifications
- Treat the reference time `'2024-01-15 00:00:00'` as the current time. No stream in the table starts after it, so the only time filter is `start_time >= '2024-01-14 00:00:00'`. The window is decided by `start_time` alone.
- A NULL `end_time` marks an ongoing stream. Exclude it. When `end_time` is present it is at or after `start_time`, so no duration is negative.
- The topic match is exact: `topic = 'gaming'`.
- Users with no qualifying stream are left out of the result. A stream whose `user_id` is NULL belongs to no user and is not counted.
- `stream_id` is unique, so every stream counts once. Two users can share a `username`; group by `user_id` so they stay separate rows.
- `users` has at most 10,000 rows and `streams` at most 100,000 rows. `topic` is a non-empty string of at most 100 characters.
```hint Turning a time gap into seconds
Subtracting two timestamps gives an interval. Check that the function you use to convert it returns the total number of seconds, not just the seconds field of the interval.
```
Overview: Write a PostgreSQL query that joins users to their streams, keeps completed gaming streams from a fixed 24-hour window, and reports each user's average stream duration in seconds. It tests timestamp arithmetic, NULL filtering, grouped averages, rounding and a deterministic sort order.