๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๐ŸŒฎ ๐Ÿ’ฌ

๐Ÿ’ป 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.