|
1 |
| -select DISTINCT author_id AS id from Views |
2 |
| -where viewer_id = author_id |
3 |
| -order by author_id asc |
| 1 | +-- Problem: |
| 2 | + |
| 3 | +/* |
| 4 | +Table: Views |
| 5 | +
|
| 6 | ++---------------+---------+ |
| 7 | +| Column Name | Type | |
| 8 | ++---------------+---------+ |
| 9 | +| article_id | int | |
| 10 | +| author_id | int | |
| 11 | +| viewer_id | int | |
| 12 | +| view_date | date | |
| 13 | ++---------------+---------+ |
| 14 | +There is no primary key (column with unique values) for this table, the table may have duplicate rows. |
| 15 | +Each row of this table indicates that some viewer viewed an article (written by some author) on some date. |
| 16 | +Note that equal author_id and viewer_id indicate the same person. |
| 17 | + |
| 18 | +
|
| 19 | +Write a solution to find all the authors that viewed at least one of their own articles. |
| 20 | +
|
| 21 | +Return the result table sorted by id in ascending order. |
| 22 | +
|
| 23 | +The result format is in the following example. |
| 24 | +
|
| 25 | + |
| 26 | +
|
| 27 | +Example 1: |
| 28 | +
|
| 29 | +Input: |
| 30 | +Views table: |
| 31 | ++------------+-----------+-----------+------------+ |
| 32 | +| article_id | author_id | viewer_id | view_date | |
| 33 | ++------------+-----------+-----------+------------+ |
| 34 | +| 1 | 3 | 5 | 2019-08-01 | |
| 35 | +| 1 | 3 | 6 | 2019-08-02 | |
| 36 | +| 2 | 7 | 7 | 2019-08-01 | |
| 37 | +| 2 | 7 | 6 | 2019-08-02 | |
| 38 | +| 4 | 7 | 1 | 2019-07-22 | |
| 39 | +| 3 | 4 | 4 | 2019-07-21 | |
| 40 | +| 3 | 4 | 4 | 2019-07-21 | |
| 41 | ++------------+-----------+-----------+------------+ |
| 42 | +Output: |
| 43 | ++------+ |
| 44 | +| id | |
| 45 | ++------+ |
| 46 | +| 4 | |
| 47 | +| 7 | |
| 48 | ++------+ |
| 49 | +*/ |
| 50 | + |
| 51 | +------------------------------------------------------------------------------- |
| 52 | + |
| 53 | +-- Solution: |
| 54 | + |
| 55 | +SELECT DISTINCT author_id AS id FROM Views |
| 56 | +WHERE viewer_id = author_id |
| 57 | +ORDER BY author_id ASC |
0 commit comments