Problem
Example
Thinking
์ฐ์ ์ฝ์ง ๋ฐฉ์ง๋ฅผ ์ํด
mysql์ ์์๋ฅผ ์ ํด์ฃผ๋(rank) ํจ์๊ฐ ์๋์ง ์ฐพ์๋ณด์๋ค!
๋คํํ ๋ด๊ฐ ์ฐพ๋ ๊ฒ์ด ๋ฐ๋ก ์์๋ค.
Example์ ์๋ ํ ์ด๋ธ ๊ทธ๋๋ก ์คํํ์ ๋์ ๊ฒฐ๊ณผ๋ก ๋น๊ตํด๋ณด์
1. RANK() : ๋์ผํ ๊ฐ์ด๋ฉด ์ค๋ณต ์์๋ฅผ ๋ถ์ฌํ๊ณ , ๋ค์ ์์๋ ํด๋น ๊ฐ์๋งํผ ๊ฑด๋๋๊ณ ๋ฐํํ๋ค.
SELETE score, RANK() OVER (ORDER BY score DESC) AS rank FROM Scores;
score | rank |
4.00 | 1 |
4.00 | 1 |
3.85 | 3 |
3.65 | 4 |
3.65 | 4 |
3.50 | 6 |
2. DENSE_RANK() : ๋์ผํ ๊ฐ์ด๋ฉด ์ค๋ณต ์์๋ฅผ ๋ถ์ฌํ๊ณ , ๋ค์ ์์๋ ์ค๋ณต ์์์ ์๊ด์์ด ์์ฐจ์ ์ผ๋ก ๋ฐํํ๋ค.
SELETE score, DENSE_RANK() OVER (ORDER BY score DESC) AS rank FROM Scores;
score | rank |
4.00 | 1 |
4.00 | 1 |
3.85 | 2 |
3.65 | 3 |
3.65 | 3 |
3.50 | 4 |
3. ROW_NUMBER() : ์ค๋ณต ๊ด๊ณ์์ด ์์ฐจ์ ์ผ๋ก ์์๋ฅผ ๋ฐํํ๋ค.
SELETE score, ROW_NUMBER() OVER (ORDER BY score DESC) AS rank FROM Scores;
score | rank |
4.00 | 1 |
4.00 | 2 |
3.85 | 3 |
3.65 | 4 |
3.65 | 5 |
3.50 | 6 |
Solve
์ ์ถ ๋ต์
SELETE score, DENSE_RANK() OVER (ORDER BY score DESC) AS rank FROM Scores;
https://leetcode.com/problems/rank-scores/
Rank Scores - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'๐ป leetcode > database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Database | 177. Nth Highest Salary (0) | 2022.01.18 |
---|---|
[LeetCode] Database | 176. Second Highest Salary (0) | 2022.01.18 |
[LeetCode] Database | 1179. Reformat Department Table (1) | 2022.01.13 |
[LeetCode] Database | 620. Not Boring Movies (0) | 2022.01.13 |
[LeetCode] Database | 596. Classes More Than 5 Students (0) | 2022.01.12 |