-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticle Views I.sql
More file actions
23 lines (22 loc) · 969 Bytes
/
Article Views I.sql
File metadata and controls
23 lines (22 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- Problem Statement: Write a solution to find all the authors that viewed at least one of their own articles.
-- Given Table: Views
--
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | article_id | int |
-- | author_id | int |
-- | viewer_id | int |
-- | view_date | date |
-- +---------------+---------+
-- There is no primary key (column with unique values) for this table, the table 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.
--
-- Approach: To find all the authors that viewed at least one of their own articles, we can query the distinct author_id from the Views table where author_id is equal to viewer_id.
--
-- SQL Solution:
SELECT DISTINCT author_id as id
FROM Views
WHERE author_id = viewer_id
ORDER BY author_id;