본문 바로가기

SQL(진짜 가끔 올라옴)

SQL 문제(LeetCode) 09.23

문제 1:

Product Sales Analysis I

 

문제 설명:

able: 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.
(sale_id, year)는 이 테이블의 기본 키(고유한 값을 가진 열의 조합)입니다.

product_id is a foreign key (reference column) to Product table.
product_id는 테이블에 대한 외래 키(참조 열)입니다 Product.

Each row of this table shows a sale on the product product_id in a certain year.
이 표의 각 행은 특정 연도의 product_id 제품에 대한 판매 실적을 보여줍니다.

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.
product_id는 이 테이블의 기본 키(고유한 값이 있는 열)입니다. 

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.
표의 각 항목에 대한 product_name, year, 를 보고하는 솔루션을 작성하세요 .pricesale_idSales

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

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

 

예시 :

Input: 
Sales table:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+ 
| 1       | 100        | 2008 | 10       | 5000  |
| 2       | 100        | 2009 | 12       | 5000  |
| 7       | 200        | 2011 | 15       | 9000  |
+---------+------------+------+----------+-------+
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100        | Nokia        |
| 200        | Apple        |
| 300        | Samsung      |
+------------+--------------+
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

 

 

문제 2:

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.
visit_id는 이 테이블의 고유한 값을 갖는 열입니다.

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.
transaction_id는 이 테이블에 대한 고유한 값을 갖는 열입니다.

This table contains information about the transactions made during the visit_id.
이 표에는 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.
거래 없이 방문한 사용자의 ID와 이러한 유형의 방문을 한 횟수를 찾는 솔루션을 작성하세요.

Return the result table sorted in any order.
임의의 순서 로 정렬된 결과 테이블을 반환합니다.

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

 

예시:

Input: 
Visits
+----------+-------------+
| visit_id | customer_id |
+----------+-------------+
| 1        | 23          |
| 2        | 9           |
| 4        | 30          |
| 5        | 54          |
| 6        | 96          |
| 7        | 54          |
| 8        | 54          |
+----------+-------------+
Transactions
+----------------+----------+--------+
| transaction_id | visit_id | amount |
+----------------+----------+--------+
| 2              | 5        | 310    |
| 3              | 5        | 300    |
| 9              | 5        | 200    |
| 12             | 1        | 910    |
| 13             | 2        | 970    |
+----------------+----------+--------+
Output: 
+-------------+----------------+
| customer_id | count_no_trans |
+-------------+----------------+
| 54          | 2              |
| 30          | 1              |
| 96          | 1              |
+-------------+----------------+

 

코드:

select v.customer_id, count(v.customer_id) as count_no_trans
from visits v left join transactions t on v.visit_id = t.visit_id
where t.transaction_id is null
group by 1

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

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