Article Views II - Problem
๐ Article Views Tracker
Imagine you're building an analytics dashboard for a popular blog platform! You have a database table called Views that tracks every time someone reads an article.
Your Mission: Find all the people who are power readers - those who viewed more than one article on the same date.
The Views table contains:
article_id: The ID of the article that was viewedauthor_id: The ID of the person who wrote the articleviewer_id: The ID of the person who viewed the articleview_date: The date when the view occurred
Key Details:
- The table may contain duplicate rows (same person viewing same article multiple times)
- When
author_idequalsviewer_id, it means the author viewed their own article - Return results sorted by
viewer_idin ascending order
Input & Output
example_1.sql โ Basic Case
$
Input:
Views table:
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date |
+------------+-----------+-----------+------------+
| 1 | 3 | 5 | 2019-08-01 |
| 3 | 4 | 5 | 2019-08-01 |
| 1 | 3 | 6 | 2019-08-02 |
| 2 | 7 | 7 | 2019-08-01 |
| 2 | 7 | 6 | 2019-08-02 |
| 4 | 7 | 1 | 2019-07-22 |
| 3 | 4 | 4 | 2019-07-21 |
| 3 | 4 | 4 | 2019-07-21 |
+------------+-----------+-----------+------------+
โบ
Output:
+------+
| id |
+------+
| 5 |
+------+
๐ก Note:
Viewer 5 viewed articles 1 and 3 on 2019-08-01, making them the only person who viewed more than one article on the same date. Note that viewer 4 viewed article 3 twice on 2019-07-21, but this counts as only one distinct article.
example_2.sql โ Multiple Power Readers
$
Input:
Views table:
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date |
+------------+-----------+-----------+------------+
| 1 | 1 | 2 | 2023-01-01 |
| 2 | 1 | 2 | 2023-01-01 |
| 3 | 2 | 3 | 2023-01-01 |
| 4 | 2 | 3 | 2023-01-01 |
| 5 | 3 | 4 | 2023-01-02 |
+------------+-----------+-----------+------------+
โบ
Output:
+------+
| id |
+------+
| 2 |
| 3 |
+------+
๐ก Note:
Both viewer 2 and viewer 3 viewed multiple articles on the same date (2023-01-01). Viewer 4 only viewed one article, so they don't qualify.
example_3.sql โ No Power Readers
$
Input:
Views table:
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date |
+------------+-----------+-----------+------------+
| 1 | 1 | 1 | 2023-01-01 |
| 1 | 1 | 1 | 2023-01-01 |
| 2 | 2 | 2 | 2023-01-02 |
| 3 | 3 | 3 | 2023-01-03 |
+------------+-----------+-----------+------------+
โบ
Output:
+------+
| id |
+------+
+------+
๐ก Note:
No viewer read more than one distinct article on any single date. Viewer 1 viewed article 1 twice on the same date, but this counts as only one distinct article.
Constraints
- 1 โค Views table rows โค 105
- article_id, author_id, viewer_id are positive integers
- view_date is in YYYY-MM-DD format
- The table may contain duplicate rows
- Results must be sorted by viewer_id in ascending order
Visualization
Tap to expand
Understanding the Visualization
1
Track All Readings
Every time someone checks out a book, we record: reader ID, book ID, and date
2
Group by Reader & Date
Organize records into groups for each (reader, date) combination
3
Count Distinct Books
For each group, count how many different books were read
4
Find Power Readers
Identify readers who read more than one book on any single day
Key Takeaway
๐ฏ Key Insight: Instead of comparing every record with every other record (expensive), we group records by (reader, date) and count distinct books in each group - much more efficient!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code