๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SQL

[SQL] - Delete, Update๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ(Leetcode - 196, 627๋ฒˆ)

๋ฐ˜์‘ํ˜•

๐Ÿ”Š ๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ์™€ ์ถœ์ฒ˜๋Š” Leetcode์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค. ๋” ๋‹ค์–‘ํ•œ SQL ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹œ๋ ค๋ฉด Leetcode ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด ๋ณด์„ธ์š”!

 

Leetcode ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์†Œ๊ฐœํ•  ๋ฌธ์ œ์™€ ํ’€์ด๋ฒ•์€ Leetcode์˜ 196๋ฒˆ, 627๋ฒˆ ๋ฌธ์ œ์ด๋‹ค. ํŠน์ดํ•˜๊ฒŒ๋„ ์ด ๋ฌธ์ œ๋“ค์€ SELECT ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ DELETE, UPDATE ๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ SELECT, DELETE, INSERT, UPDATE๋Š” DB์˜ DML(Data Manipulation Language)์ด๋‹ค. ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„ธ๋ถ€์ ์ธ ์‚ฌํ•ญ์€ Leetcode ์‚ฌ์ดํŠธ์˜ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

 

๋จผ์ € 196๋ฒˆ ๋ฌธ์ œ์™€ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ถœ๋ ฅ ํ˜•ํƒœ๋ฅผ ์‚ดํŽด๋ณด์ž.

 

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
(Person์ด๋ผ๋Š” ํ…Œ์ด๋ธ”์—์„œ ์ค‘๋ณต๋œ ์ด๋ฉ”์ผ๋“ค์„ ๋ชจ๋‘ ์‚ญ์ œํ•ด๋ผ, ์ด ๋•Œ ๊ฐ€์žฅ ์ž‘์€ id๊ฐ’์˜ ๋ฐ์ดํ„ฐ๋งŒ ๋‚จ๊ฒจ๋ผ)

Leetcode 196๋ฒˆ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ถœ๋ ฅ ํ˜•ํƒœ

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  1. DELETE ์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ SELECT ๋ฌธ์„ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐ
  2. DELETE ์™€ Self JOIN์„ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐ

์šฐ์„  1๋ฒˆ์งธ ๋ฐฉ๋ฒ•์— ํ•ด๋‹นํ•˜๋Š” ์ •๋‹ต SQL ๊ตฌ๋ฌธ์„ ์‚ดํŽด๋ณด์ž.

 

DELETE
FROM Person
WHERE Id NOT IN (SELECT * FROM(SELECT MIN(Id)
                               FROM Person
                               GROUP BY Email) AS sub)

 

์šฐ์„  ์ค‘๋ณต๋˜๋Š” ์ด๋ฉ”์ผ์„ ์ฐพ๊ธฐ ์œ„ํ•ด Email๋กœ ๊ทธ๋ฃนํ•‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋™์ผํ•œ Email ์ค‘ Id๊ฐ’์ด ๊ฐ€์žฅ ๋‚ฎ์€ ๋ฐ์ดํ„ฐ ์ด์™ธ์˜ ๋ฐ์ดํ„ฐ๋“ค์€ ๋ชจ๋‘ ์‚ญ์ œํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์œ„์™€ ๊ฐ™์ด ์„œ๋ธŒ์ฟผ๋ฆฌ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

๋‹ค์Œ์€ 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์— ํ•ด๋‹นํ•˜๋Š” ์ •๋‹ต SQL ๊ตฌ๋ฌธ์„ ๋ณด์ž.

 

DELETE p1
FROM Person AS p1 
 INNER JOIN Person AS p2 ON p1.Email = p2.Email
WHERE p1.Id > p2.Id

 

์ด ๊ตฌ๋ฌธ ์ค‘ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์€ DELETE ๊ตฌ๋ฌธ์•ˆ์—์„œ Self Join์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋™์ผํ•œ Email ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ JOIN ์„ ์‹œํ‚จ๋‹ค๋ฉด ๋‹ค์Œ ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์•„์ง„๋‹ค.

 

p1.Id p1.Email p2.Id p2.Email
1 john@example.com 1 john@example.com
3 john@example.com 1 john@example.com
2 bob@example.com 2 bob@example.com
1 john@example.com 3 john@example.com
3 john@example.com 3 john@example.com

 

๊ทธ๋ฆฌ๊ณ  ์œ„ ํ…Œ์ด๋ธ” ํ˜•ํƒœ์—์„œ p1.Id ๊ฐ’์ด p2.Id ๊ฐ’๋ณด๋‹ค ํฐ ๋ฐ์ดํ„ฐ๋“ค์„ ์‚ญ์ œ์‹œ์ผœ์ฃผ๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ํ˜•ํƒœ์ธ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

 

p1.Id p1.Email
1 john@example.com
2 bob@example.com

 

์ถ”๊ฐ€์ ์œผ๋กœ JOIN์„ ๊ตฌํ˜„ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋˜ ๋‹ค๋ฅธ SQL ํ’€์ด๋ฅผ ์ œ์‹œํ•ด ๋ณธ๋‹ค.

 

DELETE p1
FROM Person AS p1, Person AS p2
WHERE p1.Email = p2.Email AND p1.Id > p2.Id

 

๋‹ค์Œ์€ 627๋ฒˆ ๋ฌธ์ œ์™€ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ด๋‹ค.(UPDATE ๊ตฌ๋ฌธ ๊ตฌ์กฐ์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.)

 

Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update statement and no intermediate temp table.Note that you must write a single update statement, DO NOT write any select statement for this problem.
(ํ•˜๋‹จ์˜ salary ํ…Œ์ด๋ธ”์—์„œ sex ๋ณ€์ˆ˜์˜ m๊ฐ’์„ f๊ฐ’์œผ๋กœ, f๊ฐ’์„ m๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ผ. ๋‹จ, SELECT ๊ตฌ๋ฌธ์ด ์•„๋‹Œ UPDATE ๊ตฌ๋ฌธ๋งŒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.)

Leetcode 627๋ฒˆ์—์„œ ์š”๊ตฌํ•˜๋Š” ํ˜•ํƒœ

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์žˆ์–ด์„œ ์ค‘์š”ํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ•์€ IF ๊ตฌ๋ฌธ์ด๋‹ค. IF ๊ตฌ๋ฌธ์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์ž. IF ๊ตฌ๋ฌธ์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

IF( ์กฐ๊ฑดA , ์กฐ๊ฑดA๊ฐ€ ์ฐธ์ผ ๋•Œ์˜ value , ์กฐ๊ฑดA๊ฐ€ ๊ฑฐ์ง“์ผ ๋•Œ์˜ value )

์ด ๊ตฌ๋ฌธ๋งŒ ์•Œ๋ฉด ๋ฌธ์ œ ํ’€์ด๊ฐ€ ์ˆœ์กฐ๋กœ์›Œ ์ง„๋‹ค. ์ •๋‹ต SQL ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

UPDATE salary
SET sex = IF(sex = 'm', 'f', 'm')

 

์ฆ‰, sex ์นผ๋Ÿผ์˜ ๊ฐ’์ด 'm' ์ด๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค๋ฉด 'f' ๊ฐ’์„, ๋งŒ์•ฝ sex ์นผ๋Ÿผ ๊ฐ’์ด 'm'์ด ์•„๋‹ˆ๋ผ๋ฉด 'm' ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ผํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•