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:

  1. Find users whose first ever session was as a "Viewer"
  2. Count how many "Streamer" sessions these users had in total
  3. Return results ordered by streaming session count (descending), then by user_id (descending)

The Sessions table contains:

  • user_id: User identifier
  • session_start: When the session began
  • session_end: When the session ended
  • session_id: Unique session identifier
  • session_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
Viewer to Streamer Journey Analysis๐Ÿ‘€ ViewerFirst Session๐ŸŽฅ CreatorLater SessionsEvolutionWindow Function MagicFIRST_VALUE(session_type)OVER ( PARTITION BY user_id ORDER BY session_start)Adds first session type to every rowQuery Execution Flow1. Window function adds first_session_type to each row2. Filter WHERE first_session_type = 'Viewer'3. COUNT streaming sessions, GROUP BY user_id, ORDER BY count DESCResult: Users ranked by their content creation activity after starting as viewers
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.
Asked in
Meta 45 Amazon 38 Google 32 Netflix 28 YouTube 25
43.6K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen