Problem
Table: Subscriptions
+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id | int |
| start_date | date |
| end_date | date |
+-------------+------+
account_id is the primary key column for this table.
Each row of this table indicates the start and end dates of an account's subscription.
Note that always start_date < end_date.
Table: Streams
+-------------+------+
| Column Name | Type |
+-------------+------+
| session_id | int |
| account_id | int |
| stream_date | date |
+-------------+------+
session_id is the primary key column for this table.
account_id is a foreign key from the Subscriptions table.
Each row of this table contains information about the account and the date associated with a stream session.
Write an SQL query to report the number of accounts that bought a subscription in 2021
but did not have any stream session.
The query result format is in the following example.
Examples
Example 1:
|
|
Solution
Method 1 -
Intuition
We need to count accounts that started a subscription in 2021 but never streamed during their subscription. This is a classic anti-join problem: find accounts in Subscriptions with a 2021 start_date and no matching Streams.
Approach
Select accounts from Subscriptions where YEAR(start_date) = 2021, and use a LEFT JOIN to Streams on account_id. Filter for accounts where Streams.account_id IS NULL (i.e., no stream session exists for that account).
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(N + M)
where N = number of subscriptions, M = number of streams (due to join and filter). - 🧺 Space complexity:
O(1)
for SQL,O(N)
for in-memory approaches.