본문 바로가기

SQL(진짜 가끔 올라옴)

SQL 문제 (LeetCode) 09.20

문제 1:

Article Views I

 

문제 설명:

+---------------+---------+
| 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.
author_id와 viewer_id가 같은 경우 같은 사람을 나타냅니다.

Write a solution to find all the authors that viewed at least one of their own articles.
저자가 자신의 기사를 하나라도 본 적이 있는지 확인하는 솔루션을 작성하세요.

Return the result table sorted by id in ascending order.
id오름차순 으로 정렬된 결과 표를 반환합니다 .

The result format is in the following 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    |
+------+

 

 

코드:

# Write your MySQL query statement below
select distinct author_id as id
from views
where author_id = viewer_id
order by id

 

문제 2:
Invalid Tweets

 

문제 설명:

Table: Tweets

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| tweet_id       | int     |
| content        | varchar |
+----------------+---------+
tweet_id is the primary key (column with unique values) for this table.
tweet_id는 이 테이블의 기본 키(고유한 값을 갖는 열)입니다.

This table contains all the tweets in a social media app.
 이 표에는 소셜 미디어 앱의 모든 트윗이 포함되어 있습니다.

Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.
유효하지 않은 트윗의 ID를 찾는 솔루션을 작성하세요. 트윗의 내용에 사용된 문자 수가 엄격히 크면 트윗 이 유효하지 않습니다 15.

Return the result table in any order.
결과 표를 임의의 순서 로 반환합니다 .

The result format is in the following example.
결과 형식은 다음 예와 같습니다.

 

 

예시 :

Input: 
Tweets table:
+----------+-----------------------------------+
| tweet_id | content                           |
+----------+-----------------------------------+
| 1        | Let us Code                       |
| 2        | More than fifteen chars are here! |
+----------+-----------------------------------+
Output: 
+----------+
| tweet_id |
+----------+
| 2        |
+----------+

 

 

코드:

# Write your MySQL query statement below
select tweet_id from tweets where length(content) > 15

 

 

문제 3 :

Replace Employee ID With The Unique Identifier

 

 

문제 설명:

able: Employees

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
+---------------+---------+
id is the primary key (column with unique values) for this table.
id는 이 테이블의 기본 키(고유한 값을 갖는 열)입니다.

Each row of this table contains the id and the name of an employee in a company.
 이 표의 각 행에는 회사의 직원 ID와 이름이 포함되어 있습니다.

Table: EmployeeUNI

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| unique_id     | int     |
+---------------+---------+
(id, unique_id) is the primary key (combination of columns with unique values) for this table.
(id, unique_id)는 이 테이블의 기본 키(고유한 값을 가진 열의 조합)입니다.

Each row of this table contains the id and the corresponding unique id of an employee in the company.
 이 표의 각 행에는 회사 직원의 ID와 해당 고유 ID가 포함되어 있습니다.

Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.
각 사용자의 고유 ID를 표시하는 솔루션을 작성하세요 . 사용자에게 고유 ID가 없으면 그냥 show 로 바꾸세요 null.

Return the result table in any order.
결과 표를 임의의 순서로 반환합니다.

The result format is in the following example.
결과 형식은 다음 예와 같습니다.

 

 

예시 :

Input: 
Employees table:
+----+----------+
| id | name     |
+----+----------+
| 1  | Alice    |
| 7  | Bob      |
| 11 | Meir     |
| 90 | Winston  |
| 3  | Jonathan |
+----+----------+
EmployeeUNI table:
+----+-----------+
| id | unique_id |
+----+-----------+
| 3  | 1         |
| 11 | 2         |
| 90 | 3         |
+----+-----------+
Output: 
+-----------+----------+
| unique_id | name     |
+-----------+----------+
| null      | Alice    |
| null      | Bob      |
| 2         | Meir     |
| 3         | Winston  |
| 1         | Jonathan |
+-----------+----------+

 

코드:

# Write your MySQL query statement below
select Em.unique_id, E.name from Employees E left join employeeuni Em on E.id = Em.id

'SQL(진짜 가끔 올라옴)' 카테고리의 다른 글

SQL 문제(LeetCode) 09.23  (2) 2024.09.23
SQL 문제 (LeetCode)  (4) 2024.09.13
RDBMS 관계형 데이터베이스  (1) 2024.08.29
NoSQL 비관계형 데이터베이스 간단  (1) 2024.08.28
SQL 문법 정리(진행중)  (0) 2024.07.19