SQL JOIN - 생활코딩

1. 테이블 쪼개기

쪼개 지지 않은 테이블

image-20200130003545984

abcde2

image-20200130003545984

테이블 쪼개기

image-20200130003556251

image-20200130003601499

image-20200130003606630

위 2가지 경우 모두 Trade off가 존재한다. 따라서, 상황에 따라서 적절하게 활용해야 한다.

2. JOIN 간단 소개

INNER JOIN

image-20200130004527980

LEFT JOIN

image-20200130004540914

RIGHT JOIN

image-20200130004607480

FULL OUTER JOIN

abcde

3. LEFT JOIN

image-20200130004540914

# topic과 author의 left join
SELECT * FROM topic LEFT JOIN author on topic.author_id = author.aid;

image-20200130010148383

# topic author left join  결과에 profile left join
SELECT * 
FROM topic 
LEFT JOIN author 
on topic.author_id = author.aid 
LEFT JOIN profile ON author.profile_id = profile.pid;

image-20200130010511546

SELECT tid, topic.title, author_id, name, profile.title AS job_title
FROM topic 
LEFT JOIN author 
on topic.author_id = author.aid 
LEFT JOIN profile ON author.profile_id = profile.pid;

image-20200130010148383

SELECT tid, topic.title, author_id, name, profile.title AS job_title
FROM topic 
LEFT JOIN author 
on topic.author_id = author.aid 
LEFT JOIN profile ON author.profile_id = profile.pid
WHERE aid = 1;

image-20200130011128689

4. INNER JOIN

image-20200130004527980

SELECT * 
FROM topic 
INNER JOIN author 
ON topic.author_id = author.aid
INNER JOIN profile
ON author.profile_id = profile.pid;

image-20200130012515454

5. FULL OUTER JOIN

abcde

(SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid) 
UNION 
(SELECT * FROM topic RIGHT JOIN author ON topic.author_id = author.aid)

image-20200130013239226

6. EXCLUSIVE LEFT JOIN

image-20200130013516409

abcde2

SELECT * 
FROM topic LEFT JOIN author 
ON topic.author_id = author.aid 
WHERE author.aid is NULL;