Number of Accounts That Did Not Stream Problem
#2020
Description
LeetCode Problem 2020.
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.
Example 1:
Input:
Subscriptions table:
+------------+------------+------------+
| account_id | start_date | end_date |
+------------+------------+------------+
| 9 | 2020-02-18 | 2021-10-30 |
| 3 | 2021-09-21 | 2021-11-13 |
| 11 | 2020-02-28 | 2020-08-18 |
| 13 | 2021-04-20 | 2021-09-22 |
| 4 | 2020-10-26 | 2021-05-08 |
| 5 | 2020-09-11 | 2021-01-17 |
+------------+------------+------------+
Streams table:
+------------+------------+-------------+
| session_id | account_id | stream_date |
+------------+------------+-------------+
| 14 | 9 | 2020-05-16 |
| 16 | 3 | 2021-10-27 |
| 18 | 11 | 2020-04-29 |
| 17 | 13 | 2021-08-08 |
| 19 | 4 | 2020-12-31 |
| 13 | 5 | 2021-01-05 |
+------------+------------+-------------+
Output:
+----------------+
| accounts_count |
+----------------+
| 2 |
+----------------+
Explanation: Users 4 and 9 did not stream in 2021.
User 11 did not subscribe in 2021.
Solution
Approach: Anti-join on 2021 streams
An account counts when its subscription covers part of 2021 and it has no stream session dated in 2021.
- A subscription overlaps 2021 when it starts on or before
2021-12-31and ends on or after2021-01-01. (Comparing the year ofstart_dateandend_dategives the same result.) - Use
NOT EXISTSto drop accounts that have at least one stream withstream_datein 2021. Streams from other years do not matter, as account 9 and 4 in the example show.
An inner or left join followed by a WHERE on the stream date would lose accounts with no streams at all, or accounts whose other-year streams happen to pass the filter. NOT EXISTS states the condition directly. Comparing the dates against the range 2021-01-01 to 2021-12-31 also keeps the comparison usable with an index, unlike YEAR(stream_date).
SELECT COUNT(*) AS accounts_count
FROM Subscriptions AS s
WHERE s.start_date <= '2021-12-31'
AND s.end_date >= '2021-01-01'
AND NOT EXISTS (
SELECT 1
FROM Streams AS t
WHERE t.account_id = s.account_id
AND t.stream_date BETWEEN '2021-01-01' AND '2021-12-31'
);