SQL

81-85

whateveryouwish 2024. 8. 16. 17:48

81번

 

Table: 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.
 

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.

Return the result table in any order.

 

select tweet_id
from tweets
where length(content) > 15

 

이 기회에 잊고있던 length() 함수를 배울 수 있었다.

 

 

82번

 

1378. Replace Employee ID With The Unique Identifier

Table: Employees

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

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.
Each row of this table contains the id and the corresponding unique id of an employee in the company.
 

Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.

Return the result table in any order.

 

select unique_id, name
from employees e
left join employeeuni u
using (id)

 

left join 을 하면 자동으로 null 값도 조회된다.

 

83번

 

1068. Product Sales Analysis I

Table: 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.
 

Table: 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.
 

Write a solution to report the product_name, year, and price for each sale_id in the Sales table.

Return the resulting table in any order.

 

select product_name, year, price
from sales s
join product p
using (product_id)
group by sale_id

 

 

84번

 

1581. Customer Who Visited but Did Not Make Any Transactions

Table: Visits

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| visit_id    | int     |
| customer_id | int     |
+-------------+---------+
visit_id is the column with unique values for this table.
This table contains information about the customers who visited the mall.
 

Table: Transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| transaction_id | int     |
| visit_id       | int     |
| amount         | int     |
+----------------+---------+
transaction_id is column with unique values for this table.
This table contains information about the transactions made during the visit_id.
 

Write a solution to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits.

Return the result table sorted in any order.

 

select customer_id, count(*) as count_no_trans
from visits v
left join transactions
using (visit_id)
where transaction_id is null
group by customer_id

 

85번

 

197. Rising Temperature

Table: Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.
 

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

 

이 문제는 조금 어려웠다.

같은 테이블을 join 할 수 있는지 몰랐다... (하이고 ㅜㅜ)

 

select w1.id
from weather w1
join weather w2
where datediff(w1.recorddate, w2.recorddate) = 1
        and w1.temperature > w2.temperature

 

오늘 강의 한 주차가 3시간을 넘겨서 다른 것들을 하기에 시간이 부족했다.

영어 문제로 오면서 쉬워진다고 했는데, 덕분에 다른 것들에 시간을 좀 더 투자할 수 있어서 다행이다.

아티클 스터디에 정리 복습에 알고리즘도 남았는데...

그래도 SQL 5문제 10분도 안돼서 풀었으니 기분이 좀 좋다!