-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
200714 | add ERD, Data Type at 02_sql_advanced
- Loading branch information
1 parent
f1ac094
commit cfcba23
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
-- #1 서브쿼리 이용 | ||
DELETE | ||
FROM Person | ||
WHERE Id NOT IN( | ||
SELECT sub.min_id | ||
FROM ( | ||
SELECT Email, MIN(Id) AS min_id FROM Person GROUP BY email | ||
) sub) | ||
|
||
|
||
-- #2 DELETE + INNER JOIN | ||
SELECT * | ||
FROM Person p1 | ||
INNER JOIN Person p2 ON p1.Email = p2.Email | ||
-- 위 SELECT문 출력 결과 | ||
-- p1.Id p1.Email p2.Id p2.Email | ||
-- 1 john@example.com 1 john@example.com => 삭제 | ||
-- 1 john@example.com 3 john@example.com => 유지 | ||
-- 2 bob@example.com 2 bob@example.com => 유지 | ||
-- 3 john@example.com 1 john@example.com => 삭제 | ||
-- 3 john@example.com 3 john@example.com => 삭제 | ||
|
||
SELECT * | ||
FROM Person p1 | ||
INNER JOIN Person p2 ON p1.Email = p2.Email | ||
WHERE p1.Id > p2.Id | ||
-- 위 SELECT문 출력 결과 | ||
-- p1.Id p1.Email p2.Id p2.Email | ||
-- 1 john@example.com 3 john@example.com | ||
-- 2 bob@example.com 2 bob@example.com | ||
|
||
DELETE p1 | ||
FROM Person p1 | ||
INNER JOIN Person p2 ON p1.Email = p2.Email | ||
WHERE p1.Id > p2.Id |