Leetcode Problem 2020. Number of Accounts That Did Not Stream

2020. Number of Accounts That Did Not Stream

Leetcode Solutions

Using LEFT JOIN and Conditional Aggregation

  1. Start by selecting the account_id from the Subscriptions table.
  2. Use a LEFT JOIN to join the Streams table on account_id, including all subscriptions regardless of whether they have a corresponding stream.
  3. Apply a WHERE clause to filter for subscriptions that were active in 2021 (either started or ended in 2021).
  4. Group the results by account_id using the GROUP BY clause.
  5. Use a HAVING clause to ensure that for each account_id, there are no streams in 2021.
  6. Count the distinct account_ids that meet the criteria using COUNT(DISTINCT account_id).
  7. Return the count as accounts_count.
erDiagram
    Subscriptions {
        int account_id PK
        date start_date
        date end_date
    }
    Streams {
        int session_id PK
        int account_id FK
        date stream_date
    }
    Subscriptions ||--o{ Streams : has

Using NOT EXISTS Subquery

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...