SQL_코드카타(2024.01.09)_기초문제

김수경·2024년 1월 9일

코드카타

목록 보기
15/29

77. Recyclable and Low Fat Products

테이블 : Products

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| low_fats | enum |
| recyclable | enum |
+-------------+---------+
product_id는 이 테이블의 기본 키(고유 값이 있는 열)입니다.
low_fats는 유형('Y', 'N')의 ENUM(범주)입니다. 여기서 'Y'는 이 제품이 저지방임을 의미하고 'N'은 그렇지 않음을 의미합니다.
recyclable은 유형('Y', 'N')의 ENUM(범주)입니다. 여기서 'Y'는 이 제품이 재활용 가능함을 의미하고 'N'은 재활용 가능하지 않음을 의미합니다.

📜 문제

저지방이면서 재활용이 가능한 제품의 ID를 찾는 솔루션을 작성하세요.
어떤 순서로든 결과 테이블을 반환합니다.
결과 형식은 다음 예와 같습니다.

Output

+-------------+
| product_id |
+-------------+
| 1 |
| 3 |
+-------------+
1번과 3번 제품만 저지방이며 재활용이 가능합니다.

👩🏻‍💻 My Coding

[조건]

  • 저지방이면서 재활용 가능
select product_id
from products 
where low_fats = 'Y' and recyclable = 'Y'
product_id
1
3

78. Find Customer Referee

테이블 : Customer

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| referee_id | int |
+-------------+---------+
In SQL, id is the primary key column for this table.
Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.

📜 문제

Find the names of the customer that are not referred by the customer with id = 2.
Return the result table in any order.
The result format is in the following example.

Output

+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

👩🏻‍💻 My Coding

[조건]

  • id=2를 추천하지 않은 고객 이름
  • referee_id가 null 이거나 2가 아닌
select name
from Customer
where referee_id <> 2 or referee_id is null
name
Will
Jane
Bill
Zack

79. Big Countries

테이블 : World

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
+-------------+---------+
name is the primary key (column with unique values) for this table.
Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.

📜 문제

면적이 최소 300만(즉, ) 이상인 경우, 또는 3000000 km2
인구는 적어도 2,500만 명(즉, 25000000)입니다.
큰 나라 의 이름, 인구, 면적을 구하는 풀이를 작성하세요 .
어떤 순서로든 결과 테이블을 반환합니다 .

Output

+-------------+------------+---------+
| name | population | area |
+-------------+------------+---------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+-------------+------------+---------+

👩🏻‍💻 My Coding

[조건]

  • area >= 3000000 또는 population >= 25000000
select name, population, area
from World
where area >= 3000000 or population >= 25000000

80. Article Views I

테이블 : World

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

📜 문제

자신의 기사를 하나 이상 본 모든 저자를 찾는 솔루션을 작성하세요.
id오름차순 으로 정렬된 결과 테이블을 반환합니다 .
결과 형식은 다음 예와 같습니다.

Output

+------+
| id |
+------+
| 4 |
| 7 |
+------+

👩🏻‍💻 My Cording

[조건]

  • author_id와 viewr_id가 같음
  • id는 한번만, 오름차순 정렬
select author_id as id
from Views
where author_id = viewer_id 
group by author_id 
order by author_id

중복된 값을 없애주는 disticnt 사용하는 방법도 있다

select distinct author_id as id
from Views
where author_id = viewer_id 
order by author_id

81. Invalid Tweets

테이블 : Tweets

+----------------+---------+
| Column Name | Type |
+----------------+---------+
| tweet_id | int |
| content | varchar |
+----------------+---------+
tweet_id is the primary key (column with unique values) for this table.
This table contains all the tweets in a social media app.

📜 문제

잘못된 트윗의 ID를 찾는 솔루션을 작성하세요. 트윗 내용에 사용된 문자 수가 15보다 큰 경우 해당 트윗은 유효하지 않습니다.
어떤 순서로든 결과 테이블을 반환합니다 .

Output

+----------+
| 트윗ID |
+----------+
| 2 |
+----------+
설명:
트윗 1의 길이는 14입니다. 유효한 트윗입니다.
트윗 2의 길이는 32입니다. 잘못된 트윗입니다.

👩🏻‍💻My Cording

  • content의 길이가 15이상인 id
select tweet_id 
from Tweets 
where length(content) > 15

👀 DBMS별 문자의 길이 함수

  • MySQL
    • Byte 길이 : LENGTH()
    • 문자 자체의 길이 : CHAR_LENGTH() > 한글 문자를 세려면 !
  • ORACLE
    • Byte 길이 : LENGTHB()
    • 문자 자체의 길이 : LENGTH() > 한글 문자를 세려면 !

82. Replace Employee ID With The Unique Identifier

테이블 1 : Employee

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains the id and the name of an employee in a company.

테이블 2 :

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| unique_id | int |
+---------------+---------+
(id, unique_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id and the corresponding unique id of an employee in the company.

📜 문제

각 사용자의 고유 ID를 표시하는 솔루션을 작성하세요.
사용자에게 고유 ID가 없으면 null로 표시하세요.
어떤 순서 로든 결과 테이블을 반환합니다.

Output

Output:
+-----------+----------+
| unique_id | name |
+-----------+----------+
| null | Alice |
| null | Bob |
| 2 | Meir |
| 3 | Winston |
| 1 | Jonathan |
+-----------+----------+
Explanation:
Alice and Bob do not have a unique ID, We will show null instead.
The unique ID of Meir is 2.
The unique ID of Winston is 3.
The unique ID of Jonathan is 1.

👩🏻‍💻 My Cording

[조건]

  • 두 테이블 join
  • unique_id 없으면 null
select eu.unique_id as unique_id,
       ep.name as name
from Employees ep left join EmployeeUNI eu on ep.id = eu.id
unique_idname
nullAlice
nullBob
2Meir
3Winston
1Jonathan

83. Product Sales Analysis I

테이블 1 : Sales

+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
product_id is a foreign key (reference column) to Product table.
Each row of this table shows a sale on the product product_id in a certain year.
Note that the price is per unit.

테이블 2 : Product

+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+
product_id is the primary key (column with unique values) of this table.
Each row of this table indicates the product name of each product.

📜 문제

Sales 테이블의 sale_id 각각의 product_name, year, price을 나타내세요.
결과 테이블을 어떤 순서 로든 반환합니다 .

Output

+--------------+-------+-------+
| product_name | year | price |
+--------------+-------+-------+
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 5000 |
| Apple | 2011 | 9000 |
+--------------+-------+-------+

👩🏻‍💻 My Cording

[조건]

  • sales 테이블 기준으로 join
select p.product_name,
s.year,
s.price
from sales s left join product p on s.product_id = p.product_id 
product_nameyearprice
Nokia20085000
Nokia20095000
Apple20119000
profile
잘 하고 있는겨?

0개의 댓글