๐ป leetcode/database13 [LeetCode] Database | 178. Rank Scores 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,.. 2022. 3. 16. [LeetCode] Database | 177. Nth Highest Salary Problem Example Thinking n๋ฒ์งธ ํฐ ๊ฐ์ ๋ณด๋ด๋ ๊ฒ์ด๋ผ .... n๋ฒ์งธ ํฐ ๊ฐ์ด๋ฉด limit n์ผ๋ก ํด์ ์ ค ํฐ๊ฐ๋ถํฐ n๋ฒ์งธ ํฐ ๊ฐ๊น์ง n๊ฐ์ ๊ฐ์ ๊ฐ์ ธ์จ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ์ค์์ ์ ์ผ ๊ฐ์ ๊ฐ์ ธ์ค๋ฉด ๋๊ฒ ์ง? ์๊ฐํ๋ค ๊ทธ๋ฌ๋๋ wrong answer ์? null์ด ๋์์ผํ๋๋ฐ ๊ฐ์ด ๋์๋ฒ๋ ธ๋ค ํ ..๊ทธ๋ฌ๋ฉด์ ์ฐ์ .. ๊ฐ๋ค์ด ์ค๋ณต๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์ผ๋, distinct๋ก ์ค๋ณต์ ์ ๊ฑฐํด์ฃผ๊ณ n๋ฒ์งธ ํฐ ๊ฐ์ด ์์ด์ผํ๋๋ฐ ๋์๋ฒ๋ฆฐ๊ฑฐ๋ฉด .. limit n ์ผ๋ก ๋์ด์ค ์ ๋ค์ด n๋ณด๋ค ์ ์ ๊ฒฝ์ฐ์ ๊ทธ๋ ๊ฒ ๋์ฌ ์ ์์ ๊ฒ์ด๋ค. (์ : 100, 300, 200, 200 ์์ 4๋ฒ์งธ ํฐ ๊ฐ? ---distinct--> 400, 300, 200์ 4๋ฒ์งธ ํฐ ๊ฐ์ด ์๋ค. ๊ทธ๋ฌ๋ฉด ์กฐ๊ฑด์ ์ ๋ฃ์ด์ค์ผํ๋ค. .. 2022. 1. 18. [LeetCode] Database | 176. Second Highest Salary Problem Example Thinking ์ต๋๊ฐ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ์๋, 2๋ฒ์งธ๋ก ํฐ ๊ฐ์ ๊ฐ์ ธ์ค๋ ๊ฑฐ์์ ๋ฉ์นซ ๊ณ ๋ฏผํ๊ฒ ๋์๋ค. ํ .. ์ต๋๊ฐ์ ๊ฐ์ ธ์ค๋ ค๋ฉด ๋จ์ํ MAX๋ก ๊ฐ์ ธ์ค๋ฉด ๋์ง๋ง 2๋ฒ์งธ ํฐ ๊ฐ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ผ๊น? ๊ตฌ๊ธ๋ง์ ํ๋ ๋ฐ๋ก ๋ด์ฅ๋ ํจ์๊ฐ ์๋ ๊ฒ ๊ฐ์๋ค. ๊ทธ๋ผ ์ ์ผ ํฐ ๊ฐ๋ณด๋ค ์์ ๊ฐ๋ค ์ค์์ ์ ์ผ ํฐ ๊ฐ์ ๊ฐ์ ธ์ค๋ฉด ๋๊ฒ ๋ค! Solve ์ ์ถ ๋ต์ SELECT MAX(salary) AS SecondHighestSalary FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee); https://leetcode.com/problems/second-highest-salary Second Highest Salary - Lee.. 2022. 1. 18. [LeetCode] Database | 1179. Reformat Department Table Problem Example Thinking column์ ์๋ก ์์ฑํด์ค์ผ ํ๋ค. month์ ๋ง์ถฐ์ (month)_Revenue ๋ผ๊ณ ์์ฑ์ ํด์ฃผ์ ๊ทธ๋ฆฌ๊ณ id๋ก ๋ฌถ์ด์ฃผ๊ธฐ ์ํด group by ! Solve ์ ์ถ ๋ต์ SELECT id , SUM(CASE WHEN month = "Jan" THEN revenue END) AS Jan_Revenue , SUM(CASE WHEN month = "Feb" THEN revenue END) AS Feb_Revenue , SUM(CASE WHEN month = "Mar" THEN revenue END) AS Mar_Revenue , SUM(CASE WHEN month = "Apr" THEN revenue END) AS Apr_Revenue , SUM(CASE WH.. 2022. 1. 13. [LeetCode] Database | 620. Not Boring Movies Problem Example Thinking id๊ฐ ํ์์ธ ์์ด๋ค๋ง ๊ฐ์ง๊ณ ์์ผ ํ๋ค. 2๋ก ๋๋ ์ฃผ์์ ๋ ๋๋จธ์ง๊ฐ 1์ธ ์ ๋ค๋ง ๊ฐ์ง๊ณ ์ค๋ ค๋ฉด MOD(id, 2) = 1 ๊ทธ๋ฆฌ๊ณ description์ boring์ด ๋ค์ด๊ฐ์์ผ๋ฉด ์๋๊ธฐ์ description != 'boring' ํ์ ์ด ๋์ ์์๋๋ก ๋์ด๋์ด์ผ ํ๊ธฐ์ ORDER BY rating DESC Solve ์ ์ถ ๋ต์ SELECT * FROM Cinema WHERE MOD(id, 2) = 1 and description != 'boring' ORDER BY rating DESC; https://leetcode.com/problems/not-boring-movies/ Not Boring Movies - LeetCode Level up your cod.. 2022. 1. 13. [LeetCode] Database | 596. Classes More Than 5 Students Problem Example Thinking ๋จ์ํ๊ฒ group by, having ์ ๋ก ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ . Solve ์ ์ถ ๋ต์ SELECT class FROM Courses GROUP BY class HAVING count(student) >= 5; https://leetcode.com/problems/classes-more-than-5-students Classes More Than 5 Students - 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 2022. 1. 12. [LeetCode] Database | 197. Rising Temperature Problem Example Thinking ์ด๋ฒ์ ๋ฐฐ์ด ํจ์๊ฐ ์๋ค DATEDIFF DATEDIFF(date1, date2) ๋ ๊ณง date1 - date2 ์ด๋ค ์์ ๋ฌธ์ ์์๋ ๋ค์๋ ์ ๋ฐ์ดํฐ์ ๋น๊ต๋ฅผ ํด์ผํ๋ ๊ฒ์ด๋ ํ๋ฃจ ์ฐจ์ด์ด๋ค. ๊ทธ๋ผ DATEDIFF(today.recordDate, yesterday.recordDate) = 1 ์ด๋ผ๊ณ ์ ์ด์ค ์ ์๋ค. Solve ์ ์ถ ๋ต์ SELECT today.id FROM Weather AS today JOIN Weather AS yesterday ON DATEDIFF(today.recordDate, yesterday.recordDate) = 1 WHERE today.temperature > yesterday.temperature https://leetcod.. 2022. 1. 12. [LeetCode] Database | 627. Swap Salary Problem Example Thinking sex์ ๊ฐ์ m์ผ ๊ฒฝ์ฐ f ๋ก, f ์ผ ๊ฒฝ์ฐ m์ผ๋ก update๋ฅผ ํด์ผํ๋ค. ํ๋์ ๊ฐ์ ๋ณ๊ฒฝํ ๊ฒฝ์ฐ์๋ update ํ ์ด๋ธ set ์ปฌ๋ผ = ๋ณ๊ฒฝํ ๊ฐ ์ผ๋ก ํ๋ฉด ๋๋ ๋ฐ, ์ฌ๊ธฐ์๋ ์กฐ๊ฑด์ด 2๊ฐ์ด๋ค. ๊ทธ๋ด ๊ฒฝ์ฐ์๋ ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์! UPDATE ํ ์ด๋ธ๋ช SET ์ปฌ๋ผ๋ช = CASE WHEN ์ปฌ๋ผ๋ช 1 = ๊ธฐ์กด๊ฐ1 THEN ๋ณ๊ฒฝํ ๊ฐ1 WHEN ์ปฌ๋ผ๋ช 2 = ๊ธฐ์กด๊ฐ2 THEN ๋ณ๊ฒฝํ ๊ฐ2 ELSE ๋ณ๊ฒฝํ ๊ฐ3 END; IF๋ฌธ ์ฒ๋ผ ์ํ๋ ๋งํผ ์กฐ๊ฑด ์ ์ WHEN ~ THEN์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์์ ์กฐ๊ฑด์ ๋ชจ๋ ์ถฉ์กฑํ์ง ์์ ์ ๋ฐํํ ๊ฐ์ ELSE ์ ๋ฃ์ด์ฃผ๊ณ ๋ง์ง๋ง์ END๋ก ๋ง๋ฌด๋ฆฌํ๊ธฐ Solve ์ ์ถ ๋ต์ update Salary set sex = C.. 2022. 1. 5. [LeetCode] Database | 196. Delete Duplicate Emails Problem Example Thinking ์ฒ์์ dictinct๋ก ์ค๋ณต ์ ๊ฑฐ๋ฅผ ํ๋ คํ์ง๋ง ์คํจ email ๊ฐ์ ์ ๋ค๋ผ๋ฆฌ inner join ํด์ฃผ๊ณ id๊ฐ ํฐ๊ฒ๋ค์ ๊ทธ๋ฅ ์ญ์ ํด๋ฒ๋ ธ๋ค. ๊ทธ๋ฌ๋๋ ์ฑ๊ณต Solve ์ ์ถ ๋ต์ DELETE p1 FROM Person p1 INNER JOIN Person p2 ON p1.email = p2.email AND p1.id > p2.id; https://leetcode.com/problems/delete-duplicate-emails/ Delete Duplicate Emails - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowle.. 2022. 1. 3. ์ด์ 1 2 ๋ค์