๐ป leetcode17 [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. [LeetCode] Database | 183. Customers Who Never Order Problem Example Thinking ์ฐ์ LEFT JOIN์ผ๋ก Customers์ ๋์ํ๋ Order์ ๋ณด๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์จ๋ค. Order ์ ๋ณด๊ฐ ์์ ๊ฒฝ์ฐ์๋ Null๋ก ํ์๊ฐ ๋๋, LEFT JOINํ ํ ์ด๋ธ์์ where ์ ๋ก Order ํ ์ด๋ธ์ id๋ customerId๊ฐ null์ธ ์กฐ๊ฑด์ ์ถ๊ฐํด์ฃผ์ Solve ์ ์ถ ๋ต์ select c.name as Customers from Customers as c LEFT JOIN Orders as o on c.id = o.customerId where o.customerId is null # where o.id is null ๋ ๊ฐ๋ฅ https://leetcode.com/problems/customers-who-never-order/submission.. 2022. 1. 2. [LeetCode] Database | 182. Duplicate Emails Problem Example Thinking ํ ์ด๋ธ์์ ์ค๋ณต์ด ๋๋ ์ด๋ฉ์ผ์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค. ์ค๋ณต๋ ์ด๋ฉ์ผ์ GROUP BY๋ก ๋ฌถ์ด์ฃผ๊ณ HAVING์ ์ฌ์ฉํด์ count ๊ฐ์ด 1 ์ด๊ณผ (ํน์ 2 ์ด์)์ธ email ๊ฐ์ ๊ณจ๋ผ์ฃผ์ Solve ์ ์ถ ๋ต์ select email as Email from Person GROUP BY email HAVING COUNT(*) > 1; https://leetcode.com/problems/duplicate-emails/submissions/ Duplicate Emails - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge.. 2022. 1. 2. [LeetCode] Database | 181. Employees Earning More Than Their Manager Problem Example Thinking input ํ ์ด๋ธ์ ํ์ธํ๋ฉด Joe์ ๋งค๋์ ๋ managerId๊ฐ 3์ด๋ฏ๋ก id๊ฐ 3์ธ Sam์ด Joe์ ๋งค๋์ ์ด๊ณ , Henry๋ ๋๊ฐ์ ๊ณผ์ ์ ๊ฑฐ์ณ Max๊ฐ ๋งค๋์ ์์ ์ ์ ์๋ค. managerId๊ฐ null์ธ Sam๊ณผ Max๋ ๋งค๋์ ์ด๋ค. ๋์ผ ํ ์ด๋ธ ๊ฐ์ ์กฐ์ธ์ ์ํด์ self join์ ์ฌ์ฉํ์! Solve ์ ์ถ ๋ต์ select e1.name as Employee from Employee as e1 INNER JOIN Employee as e2 on e1.managerId = e2.id where e1.salary > e2.salary https://leetcode.com/problems/employees-earning-more-than-their-.. 2022. 1. 2. [LeetCode] Database | 175. Combine Two Tables Problem Example Thinking Output์ ํ์ธํ๋ฉด person table์ ์๋ ํ์๋ค์ ๋์ดํ๊ณ ๊ทธ ํ์์ ํด๋นํ๋ (personId๊ฐ ๋์ผํ) city, state ์ ๋ณด๋ฅผ ๊ฐ์ ธ์์ ํ ์ด๋ธ์ ์์ฑ์์ผฐ๋ค. Address ์ ๋ณด๊ฐ ์๋ ํ์์ผ ๊ฒฝ์ฐ์๋ Null๋ก ํ์ ํด์ฃผ์๋ค. ํ ์ด๋ธ์ด 2๊ฐ ์์ผ๋ join์ ํด์ค ๊ฒ์ด๋ฉฐ, ์กฐ์ธ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ(Person table) ์ ๋ณด๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์ฌ ๊ฒ์ด๊ธฐ์ LEFT JOIN์ ์ฌ์ฉํด๋ณด์. Solve ์ ์ถ ๋ต์ select p.firstName, p.lastName, a.city, a.state from Person p left join Address a on p.personId = a.personId https://leetcode.com/problem.. 2021. 12. 29. ์ด์ 1 2 ๋ค์