본문 바로가기

코딩스터디

Leetcode SQL : Not boring Movies, Article Views

# 문제출처
https://leetcode.com/problems/not-boring-movies/ 

 

# 문제1

 

Table: Cinema
+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| id             | int      |
| movie          | varchar  |
| description    | varchar  |
| rating         | float    |
+----------------+----------+
id is the primary key for this table.
Each row contains information about the name of a movie, its genre, and its rating.
rating is a 2 decimal places float in the range [0, 10]
id는 이 테이블의 기본 키입니다.
각 행에는 영화 이름, 장르, 등급에 대한 정보가 포함되어 있습니다.
등급은 [0, 10] 범위의 부동소수점 2자리입니다.


Write an SQL query to report the movies with an odd-numbered ID and a description that is not "boring".
Return the result table ordered by rating in descending order.
홀수 ID와 설명이 "boring"이 아닌 영화를 보고하는 SQL 쿼리를 작성하세요.
등급별로 내림차순으로 정렬된 결과 테이블을 반환합니다.


Example:
Input: 
Cinema table:
+----+------------+-------------+--------+
| id | movie      | description | rating |
+----+------------+-------------+--------+
| 1  | War        | great 3D    | 8.9    |
| 2  | Science    | fiction     | 8.5    |
| 3  | irish      | boring      | 6.2    |
| 4  | Ice song   | Fantacy     | 8.6    |
| 5  | House card | Interesting | 9.1    |
+----+------------+-------------+--------+
Output: 
+----+------------+-------------+--------+
| id | movie      | description | rating |
+----+------------+-------------+--------+
| 5  | House card | Interesting | 9.1    |
| 1  | War        | great 3D    | 8.9    |
+----+------------+-------------+--------+
Explanation: 
We have three movies with odd-numbered IDs: 1, 3, and 5.
The movie with ID = 3 is boring so we do not include it in the answer.
설명:
홀수 ID인 1, 3, 5의 영화 3개가 있습니다.
ID = 3인 영화는 'boring'이므로 답변에 포함하지 않습니다.
*/

# [SETTING]
USE practice;
DROP TABLE Cinema;
CREATE TABLE Cinema (
  id INT,
  movie VARCHAR(255),
  description VARCHAR(255),
  rating FLOAT(2, 1)
);
INSERT INTO
  Cinema (id, movie, description, rating)
VALUES
  (1, 'War', 'great 3D', 8.9),
  (2, 'Science', 'fiction', 8.5),
  (3, 'irish', 'boring', 6.2),
  (4, 'Ice song', 'Fantacy', 8.6),
  (5, 'House card', 'Interesting', 9.1);
  
SELECT *
FROM Cinema;

# [KEY]
# MOD(숫자, 나눌 값)=나머지
# 홀수 판별: MOD(id,2)=1 <-> 짝수 판별: MOD(id,2)=0

# [MYSQL1]
SELECT *
FROM Cinema
WHERE MOD(id, 2) = 1
  AND description != 'boring'
ORDER BY rating DESC;

# [MYSQL2]
SELECT *
FROM Cinema
WHERE id % 2 = 1
  AND description != 'boring'
ORDER BY rating DESC;

 

TIP: MOD = % 
홀수판별 id % 2 = 1 <-> 짝수판별 id % 2 = 0

 

 

# 문제2

/*
https://leetcode.com/problems/article-views-i/ 

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.
이 테이블에는 기본 키가 없습니다. 중복된 행이 있을 수 있습니다.
이 표의 각 행은 일부 독자가 특정 날짜에 기사(특정 작가가 쓴)를 봤음을 나타냅니다.
동일한 author_id와 viewer_id는 동일한 사람을 나타냅니다.


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.
자신의 기사를 최소 한 번 이상 본 작가를 찾는 SQL 쿼리를 작성하세요.
id를 기준으로 오름차순으로 정렬된 결과 테이블을 반환합니다.


Example:
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    |
+------+
*/

# [SETTING]
USE practice;
DROP TABLE Views;
CREATE TABLE Views (
  article_id INT,
  author_id INT,
  viewer_id INT,
  view_date DATE
);
INSERT INTO
  Views (article_id, author_id, viewer_id, view_date)
VALUES
  (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');
SELECT *
FROM Views;

# [KEY]
# 'There is no primary key for this table, it may have duplicate rows.' : DISTINCT 처리 필요
# Primary Key(=PK): 중복되어 나타날 수 없는 단일 값(unique) (e.g. 우리나라 국민의 주민등록번호)

# 쿼리 실행 순서: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT

# [WRONG]
SELECT
  author_id AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;

# [MYSQL] 
# DISTINCT 필요
SELECT
  DISTINCT (author_id) AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;

 

TIP : primary key 가 없다고 하면 distinct 처리 필요.

where은 from 다음에 온다!

728x90