

LeetCode에서 뭐 풀까 고민될때는
저 셔플 버튼을 누르면 아무거나 띄어줍니다.
그렇게 최소 3문제씩이라도 꾸준히 풀겠습니다.
난이도
Medium
문제
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.
Each row records a sale of a product in a given year.
A product may have multiple sales entries in the same year.
Note that the per-unit price.
Write a solution to find all sales that occurred in the first year each product was sold.
For each product_id, identify the earliest year it appears in the Sales table.
Return all sales entries for that product in that year.
Return a table with the following columns: product_id, first_year, quantity, and price.
Return the result in any order.
Example 1:
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 |
+---------+------------+------+----------+-------+
Output:
+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+
| 100 | 2008 | 10 | 5000 |
| 200 | 2011 | 15 | 9000 |
+------------+------------+----------+-------+
핵심
윈도우 함수(RANK) 로직 단계
순위 매기기: product_id별로 그룹을 나누고(PARTITION BY)
year가 낮은 순서대로(ORDER BY ASC) 순위 부여.임시 테이블 생성: 윈도우 함수는 WHERE 절에서 직접 사용할 수 없음
순위가 포함된 결과물을 서브쿼리(나 WITH 절)로 감싸기.필터링: 바깥쪽 쿼리에서 순위가 1인 행만 선택하기.
코드
SELECT
product_id,
year AS first_year,
quantity,
price
FROM (
SELECT
product_id,
year,
quantity,
price,
RANK() OVER (PARTITION BY product_id ORDER BY year) as rnk
FROM Sales
) t
WHERE rnk = 1;
난이도
Easy
문제
Table: 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.
A country is big if:
it has an area of at least three million (i.e., 3000000 km2), or
it has a population of at least twenty-five million (i.e., 25000000).
Write a solution to find the name, population, and area of the big countries.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
World table:
+-------------+-----------+---------+------------+--------------+
| name | continent | area | population | gdp |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
| Albania | Europe | 28748 | 2831741 | 12960000000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
| Andorra | Europe | 468 | 78115 | 3712000000 |
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
+-------------+-----------+---------+------------+--------------+
Output:
+-------------+------------+---------+
| name | population | area |
+-------------+------------+---------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+-------------+------------+---------+
핵심
너무 기본 문제라 넘어갑니다..
코드
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;
난이도
Medium
문제
Table: Employee
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.
Write a solution to find the nth highest distinct salary from the Employee table. If there are less than n distinct salaries, return null.
The result format is in the following example.
Example 1:
Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
n = 2
Output:
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
Example 2:
Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
n = 2
Output:
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| null |
+------------------------+
핵심
윈도우 함수 DENSE_RANK() 활용, 중복된 급여만 잘 처리
코드
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
SELECT DISTINCT salary
FROM (
SELECT
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) as rnk
FROM Employee
) t
WHERE rnk = N
);
END
LeetCode도 구독해야만 더 풀 수 있는 문제가 많아서
다 풀면 또 넘어가야할 듯 합니다...
이렇게 자꾸 구독 시키면 차라리 AI한테 코딩 테스트 문제 만들어달라고 하고, 그걸로 연습하는 게 낫지 않을까...