테이블 : 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번 제품만 저지방이며 재활용이 가능합니다.
[조건]
select product_id
from products
where low_fats = 'Y' and recyclable = 'Y'
| product_id |
|---|
| 1 |
| 3 |
테이블 : 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 |
+------+
[조건]
select name
from Customer
where referee_id <> 2 or referee_id is null
| name |
|---|
| Will |
| Jane |
| Bill |
| Zack |
테이블 : 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 |
+-------------+------------+---------+
[조건]
select name, population, area
from World
where area >= 3000000 or population >= 25000000
테이블 : 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 |
+------+
[조건]
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
테이블 : 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입니다. 잘못된 트윗입니다.
select tweet_id
from Tweets
where length(content) > 15
LENGTH()CHAR_LENGTH() > 한글 문자를 세려면 !LENGTHB()LENGTH() > 한글 문자를 세려면 !테이블 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.
[조건]
select eu.unique_id as unique_id,
ep.name as name
from Employees ep left join EmployeeUNI eu on ep.id = eu.id
| unique_id | name |
|---|---|
| null | Alice |
| null | Bob |
| 2 | Meir |
| 3 | Winston |
| 1 | Jonathan |
테이블 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 |
+--------------+-------+-------+
[조건]
select p.product_name,
s.year,
s.price
from sales s left join product p on s.product_id = p.product_id
| product_name | year | price |
|---|---|---|
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 5000 |
| Apple | 2011 | 9000 |