[1148] Article Views I

2022. 6. 29. 03:02·SQL/LeetCode

Table: Views

+---------------+---------+
| 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.

Return the result table sorted by id in ascending order.

The query result format is in the following example.

 

Example 1:

Input: 
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 |
+------------+-----------+-----------+------------+
Output: 
+------+
| id   |
+------+
| 4    |
| 7    |
+------+

 

문제 조건 

자신의 기사 중 하나 이상을 본 모든 작성자를 출력하시오.

id를 기준으로 오름차순 하시오 

 

정답 쿼리 

- 문제 조건은 author_id = viewer_id가 되어야 한다는 뜻으로 이해했습니다.

  이에, where 절에 author_id = viewer_id가 되는 것들만 필터링 하고 

  기사가 여러개 일 수 있으니, author_id를 unique하게 뽑아낸 후, id값을 기준으로 오름차순 함 

 

select distinct author_id as id 
from views
where author_id = viewer_id 
order by id asc

'SQL > LeetCode' 카테고리의 다른 글

[574] Winning Candidate  (0) 2022.06.29
[570#_MEDIUM] Managers with at Least 5 Direct Reports  (0) 2022.06.29
[1142] User Activity for the Past 30 Days II  (0) 2022.06.28
[1141] User Activity for the Past 30 Days I  (0) 2022.06.28
[550] Game Play Analysis IV  (0) 2022.06.26
'SQL/LeetCode' 카테고리의 다른 글
  • [574] Winning Candidate
  • [570#_MEDIUM] Managers with at Least 5 Direct Reports
  • [1142] User Activity for the Past 30 Days II
  • [1141] User Activity for the Past 30 Days I
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    group by
    easy
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    프로그래머스
    프리미엄
    Hard
    패캠챌린지
    medium
    파이썬을활용한시계열데이터분석
    LeetCode
    solvesql
    패스트캠퍼스후기
    패스트캠퍼스
    시계열데이터분석
    MySQL
    HACKER_RANK
    해커랭크
    SQL
    직장인자기계발
    직장인인강
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[1148] Article Views I
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.