Number of Accounts That Did Not Stream - Problem

๐ŸŽฌ Streaming Service Analytics

You're working as a data analyst for a popular streaming service! Your company wants to understand subscriber engagement patterns to optimize their business strategy.

You have access to two important datasets:

  • Subscriptions Table: Contains account subscription periods with account_id, start_date, and end_date
  • Streams Table: Records every streaming session with session_id, account_id, and stream_date

Your mission: Find how many accounts purchased a subscription in 2021 but never actually used the service (had zero streaming sessions).

This metric is crucial for understanding subscription abandonment - customers who pay but don't engage with the platform. The marketing team needs this data to improve user onboarding and reduce churn!

Return a single number representing the count of inactive subscribers from 2021.

Input & Output

Basic Example
$ Input: Subscriptions: +------------+------------+------------+ | account_id | start_date | end_date | +------------+------------+------------+ | 1 | 2021-01-01 | 2021-12-31 | | 2 | 2021-03-15 | 2021-06-15 | | 3 | 2020-12-01 | 2021-02-28 | +------------+------------+------------+ Streams: +------------+------------+-------------+ | session_id | account_id | stream_date | +------------+------------+-------------+ | 101 | 1 | 2021-02-14 | | 102 | 1 | 2021-03-20 | +------------+------------+-------------+
โ€บ Output: 1
๐Ÿ’ก Note: Accounts 1 and 2 subscribed in 2021. Account 1 has streaming sessions, but account 2 has no streams. Account 3 started in 2020, so it's not counted. Result: 1 inactive account.
No Inactive Accounts
$ Input: Subscriptions: +------------+------------+------------+ | account_id | start_date | end_date | +------------+------------+------------+ | 1 | 2021-01-01 | 2021-12-31 | | 2 | 2021-06-01 | 2021-12-31 | +------------+------------+------------+ Streams: +------------+------------+-------------+ | session_id | account_id | stream_date | +------------+------------+-------------+ | 101 | 1 | 2021-02-14 | | 102 | 2 | 2021-07-01 | +------------+------------+-------------+
โ€บ Output: 0
๐Ÿ’ก Note: Both accounts that subscribed in 2021 have streaming activity, so no accounts are inactive.
All Accounts Inactive
$ Input: Subscriptions: +------------+------------+------------+ | account_id | start_date | end_date | +------------+------------+------------+ | 1 | 2021-01-01 | 2021-12-31 | | 2 | 2021-06-01 | 2021-12-31 | | 3 | 2021-09-01 | 2021-12-31 | +------------+------------+------------+ Streams: +------------+------------+-------------+ | session_id | account_id | stream_date | +------------+------------+-------------+ | 101 | 4 | 2021-02-14 | +------------+------------+-------------+
โ€บ Output: 3
๐Ÿ’ก Note: All three accounts subscribed in 2021, but none of them have any streaming sessions (account 4 in streams didn't subscribe in 2021). All are inactive.

Visualization

Tap to expand
๐ŸŽญ Theater Season Ticket Analysis2021 TicketsAliceBobCarolShow AttendanceAlice - RomeoAlice - HamletCarol - MacbethCOMPARENo-Show AnalysisAlice: โœ“ ActiveBob: โŒ InactiveCarol: โœ“ ActiveFinal CountInactive Subscribers: 1(Bob bought ticket but never attended)SQL Translation:1. Filter subscriptions WHERE YEAR(start_date) = 20212. LEFT JOIN with streams ON account_id3. Filter WHERE stream_id IS NULL4. COUNT(DISTINCT account_id)
Understanding the Visualization
1
Identify 2021 Season Ticket Holders
Find all customers who bought season passes in 2021
2
Check Attendance Records
For each ticket holder, see if they appear in any show attendance logs
3
Find No-Shows
Identify ticket holders with zero attendance
4
Count the Result
Return the total number of inactive ticket holders
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN with NULL filtering is the most efficient way to find records that exist in one table but not another - a common pattern in data analysis for identifying inactive users, missing relationships, or gaps in data.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Single pass through subscriptions (n) and streams (m) with hash join

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for hash table of smaller relation (k accounts)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค account_id โ‰ค 106
  • start_date < end_date
  • Dates are in format YYYY-MM-DD
  • session_id is unique in Streams table
  • account_id in Streams is a foreign key from Subscriptions
Asked in
Netflix 45 Amazon 38 Meta 32 Google 28 Spotify 24
31.2K Views
High Frequency
~12 min Avg. Time
1.5K 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