Viewers Turned Streamers - Problem

Given a Sessions table that tracks user streaming and viewing activity, find the number of streaming sessions for users whose first session was as a viewer.

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'

Return the result ordered by count of streaming sessions descending, then user_id descending.

Table Schema

Sessions
Column Name Type Description
user_id int User identifier
session_start datetime Session start timestamp
session_end datetime Session end timestamp
session_id PK int Unique session identifier
session_type enum Either 'Viewer' or 'Streamer'
Primary Key: session_id
Note: Tracks both viewing and streaming sessions for users

Input & Output

Example 1 — Mixed User Types
Input Table:
user_id session_start session_end session_id session_type
123 2024-01-01 10:00:00 2024-01-01 11:00:00 1 Viewer
123 2024-01-02 10:00:00 2024-01-02 11:00:00 2 Streamer
123 2024-01-03 10:00:00 2024-01-03 11:00:00 3 Streamer
456 2024-01-01 12:00:00 2024-01-01 13:00:00 4 Streamer
456 2024-01-02 12:00:00 2024-01-02 13:00:00 5 Viewer
789 2024-01-01 14:00:00 2024-01-01 15:00:00 6 Viewer
Output:
user_id sessions_count
123 2
💡 Note:

User 123's first session was as a Viewer (2024-01-01), so we count their streaming sessions (2). User 456's first session was as a Streamer, so they're excluded. User 789 started as a Viewer but has no streaming sessions.

Example 2 — Multiple Qualifying Users
Input Table:
user_id session_start session_end session_id session_type
100 2024-01-01 09:00:00 2024-01-01 10:00:00 1 Viewer
100 2024-01-02 09:00:00 2024-01-02 10:00:00 2 Streamer
200 2024-01-01 11:00:00 2024-01-01 12:00:00 3 Viewer
200 2024-01-02 11:00:00 2024-01-02 12:00:00 4 Streamer
200 2024-01-03 11:00:00 2024-01-03 12:00:00 5 Streamer
200 2024-01-04 11:00:00 2024-01-04 12:00:00 6 Streamer
Output:
user_id sessions_count
200 3
100 1
💡 Note:

Both users started as viewers. User 200 has 3 streaming sessions and user 100 has 1. Results are ordered by sessions_count DESC, user_id DESC.

Example 3 — No Qualifying Users
Input Table:
user_id session_start session_end session_id session_type
111 2024-01-01 10:00:00 2024-01-01 11:00:00 1 Streamer
222 2024-01-01 12:00:00 2024-01-01 13:00:00 2 Streamer
Output:
user_id sessions_count
💡 Note:

No users started as viewers - both began as streamers, so no results are returned.

Constraints

  • 1 ≤ user_id ≤ 10^6
  • session_id is unique for each row
  • session_type is either 'Viewer' or 'Streamer'
  • session_start < session_end for all rows

Visualization

Tap to expand
Viewers Turned Streamers INPUT: Sessions Table user_id type date 101 viewer 2024-01 101 streamer 2024-02 101 streamer 2024-03 102 streamer 2024-01 103 viewer 2024-01 103 streamer 2024-02 Users with sessions: 101 3 sessions 102 1 session 103 2 sessions Blue = Started as viewer Red = Started as streamer ALGORITHM STEPS 1 Find First Session GROUP BY user_id MIN(session_date) SELECT user_id, MIN(date) as first 2 Filter Viewers Only WHERE first session type = 'viewer' WHERE type = 'viewer' 3 Count Streaming Count streamer sessions for qualified users 4 Order Results ORDER BY count DESC, user_id DESC FINAL RESULT user_id stream_count 101 2 103 1 Users who started as viewers: 101 First: viewer (2024-01) Streams: 2024-02, 2024-03 Count: 2 103 First: viewer (2024-01) Streams: 2024-02 Count: 1 X User 102: Started as streamer - EXCLUDED Key Insight: Use a subquery or CTE to first identify each user's earliest session type. Then filter for users whose first session was 'viewer', and count only their subsequent 'streamer' sessions. The ordering ensures most active converted streamers appear first, with user_id as tiebreaker for consistent results. TutorialsPoint - Viewers Turned Streamers | Optimal Solution
Asked in
Twitch 28 YouTube 15 Meta 12
23.4K Views
Medium Frequency
~18 min Avg. Time
892 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