Article Views I Problem
Description
LeetCode Problem 1148.
Table: Views
1
2
3
4
5
6
7
8
9
10
11
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| article_id | int |
| author_id | int |
| viewer_id | int |
| view_date | date |
+---------------+---------+
There is no primary key for this table, it may have duplicate rows.
Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
Note that equal author_id and viewer_id indicate the same person.
Write an SQL query to find all the authors that viewed at least one of their own articles, sorted in ascending order by their id.
The query result format is in the following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Views table:
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date |
+------------+-----------+-----------+------------+
| 1 | 3 | 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 |
+------------+-----------+-----------+------------+
Result table:
+------+
| id |
+------+
| 4 |
| 7 |
+------+
MySQL Solution
1
2
3
4
5
select author_id as id
from Views
where author_id = viewer_id
group by id
order by id
LeetCode Database - Easy
LeetCode 1050
LeetCode 1068
LeetCode 1069
LeetCode 1075
LeetCode 1076
LeetCode 1082
LeetCode 1083
LeetCode 1084
LeetCode 1113
LeetCode 1141
LeetCode 1142
LeetCode 1148
LeetCode 1173
LeetCode 1179
LeetCode 1211
LeetCode 1241
LeetCode 1251
LeetCode 1280
LeetCode 1294
LeetCode 1303
LeetCode 1322
LeetCode 1327
LeetCode 1350
LeetCode 1378
More LeetCode Database
MySQL Tutorials