Viewers Turned Streamers - Problem
Imagine you're analyzing user behavior on a streaming platform where users can either watch streams or create their own content. You have a database table containing all user sessions with their activities.
Your task is to identify users who started as viewers but eventually became streamers. Specifically, you need to:
- Find users whose first ever session was as a "Viewer"
- Count how many "Streamer" sessions these users had in total
- Return results ordered by streaming session count (descending), then by user_id (descending)
The Sessions table contains:
user_id: User identifiersession_start: When the session begansession_end: When the session endedsession_id: Unique session identifiersession_type: Either 'Viewer' or 'Streamer'
This problem helps identify user engagement patterns and potential content creators who evolved from passive consumers to active contributors.
Input & Output
example_1.sql โ Basic Example
$
Input:
Sessions table:
| user_id | session_start | session_end | session_id | session_type |
|---------|--------------|-------------|------------|-------------|
| 1 | 2023-11-01 | 2023-11-01 | 101 | Viewer |
| 1 | 2023-11-02 | 2023-11-02 | 102 | Streamer |
| 1 | 2023-11-03 | 2023-11-03 | 103 | Streamer |
| 2 | 2023-11-01 | 2023-11-01 | 201 | Streamer |
| 2 | 2023-11-02 | 2023-11-02 | 202 | Viewer |
โบ
Output:
| user_id | streaming_sessions |
|---------|-------------------|
| 1 | 2 |
๐ก Note:
User 1's first session was as a Viewer (2023-11-01), then had 2 Streamer sessions. User 2's first session was as a Streamer, so they're excluded from results.
example_2.sql โ Multiple Users
$
Input:
Sessions table:
| user_id | session_start | session_end | session_id | session_type |
|---------|--------------|-------------|------------|-------------|
| 3 | 2023-11-01 | 2023-11-01 | 301 | Viewer |
| 3 | 2023-11-02 | 2023-11-02 | 302 | Streamer |
| 4 | 2023-11-01 | 2023-11-01 | 401 | Viewer |
| 5 | 2023-11-01 | 2023-11-01 | 501 | Viewer |
| 5 | 2023-11-02 | 2023-11-02 | 502 | Viewer |
โบ
Output:
| user_id | streaming_sessions |
|---------|-------------------|
| 5 | 0 |
| 4 | 0 |
| 3 | 1 |
๐ก Note:
Users 3, 4, and 5 all started as viewers. User 3 has 1 streaming session, users 4 and 5 have 0. Results ordered by streaming_sessions DESC, then user_id DESC.
example_3.sql โ Edge Case
$
Input:
Sessions table:
| user_id | session_start | session_end | session_id | session_type |
|---------|--------------|-------------|------------|-------------|
| 6 | 2023-11-01 | 2023-11-01 | 601 | Viewer |
| 7 | 2023-11-01 | 2023-11-01 | 701 | Streamer |
โบ
Output:
| user_id | streaming_sessions |
|---------|-------------------|
| 6 | 0 |
๐ก Note:
User 6 started as viewer but never streamed (0 streaming sessions). User 7 started as streamer, so excluded. Even users with 0 streaming sessions should be included if they started as viewers.
Constraints
- 1 โค number of sessions โค 104
- 1 โค user_id โค 103
- session_start < session_end for all sessions
- session_type is either 'Viewer' or 'Streamer'
- session_id is unique across all sessions
- Each user has at least one session
Visualization
Tap to expand
Understanding the Visualization
1
Identify First Session
Use FIRST_VALUE() to mark each user's chronologically first session type
2
Filter Viewer-First Users
Keep only users whose journey started with watching content
3
Count Streaming Activity
Aggregate streaming sessions to measure content creation volume
4
Rank by Activity
Order by streaming count to highlight most active creator converts
Key Takeaway
๐ฏ Key Insight: Window functions enable complex user journey analysis in a single query, efficiently identifying behavioral transitions from passive consumption to active content creation.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code