Database/Maria,Mysql

생활코딩 - SQL JOIN 학습 정리

bluebamus 2022. 1. 16.

https://edu.goorm.io/learn/lecture/14441/%EC%83%9D%ED%99%9C%EC%BD%94%EB%94%A9-sql-join

 

생활코딩 - SQL JOIN - 구름EDU

생활코딩과 함께 관계형데이터베이스에서 JOIN 기능을 활용하는 방법을 알아봅시다.

edu.goorm.io

https://github.com/egoing/sql-join

 

GitHub - egoing/sql-join

Contribute to egoing/sql-join development by creating an account on GitHub.

github.com

https://sql-joins.leopard.in.ua/

 

SQL Joins Visualizer

Please select how do you want to do SQL JOIN between two table

sql-joins.leopard.in.ua

https://dataschool.com/

 

The Data School by Chartio

A community driven school to help people create data driven organizations. Learn SQL, Dashboard Design, and Analysis techniques and more.

dataschool.com

join 정리 1
join 정리 2

    1) 테이블 쪼개기

        1-1) 문제점 : name의 egoing이라는 사람의 이름을 변경할 경우, 1000만건의 데이터가 있다면 모든 테이블을
                         검사하여 동일한 데이터를 확인, 변경하는 과정을 거쳐야 한다.

        1-2) 문제점 : comment 테이블에서 topic과 동일한 name, city, job_title, job_description을 저징하는 경우,

                         해당 데이터는 중복이 발생한다.

        1-1) 해결 방안 : topic 테이블과 comment 테이블에 공통으로 사용되는 중복 데이터를 분리하여 새로운 테이블로

                             만든고 id로 관계를 정의한다.

    1) LEFT (OUTER) JOIN

        1-1) 왼쪽을 기준으로 존재하는 데이터를 출력하고 매칭되는 데이터가 없으면 NULL을 출력한다

            - 목표 : 분리된 3개의 테이블 topic, author, profile을 하나의 테이블로 join을 함

            - 테이블 기본 구조

            - Mysql Query : topic 출력, topic & author 출력, topic & author & profile 출력

SELECT * FROM topic;

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

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

            - 완성된 테이블 구조 :

            - Mysql Query : topic & author & profile 출력에서 컬럼명 변경 및 원하는 열만 선택 출력 

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;

            - 완성된 테이블 구조 :

            - Mysql Query : topic & author & profile의 컬럼명 변경 및 원하는 열만 선택한 결과에서 aid 1만 출력

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

            - 완성된 테이블 구조 :

    2) RIGHT (OUTER) JOIN

        1-1) LEFT (OUTER) JOIN과 같은 동작을 한다. 다만, 방향만 다를 뿐이다.

 

    3) INNER JOIN

        1-1) 매우 엄격한 출력 조건으로 양쪽에 다 있는 데이터만 출력한다.

            - 테이블 기본 구조 :

            - Mysql Query : topic, author 테이블을 inner join 한 결과를 출력한다.

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

            - 완성된 테이블 구조 :

            - Mysql Query : topic, author, profile 테이블을 inner join 한 결과를 출력한다.

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

            - 완성된 테이블 구조 :

    4) FULL JOIN

        1-1) join 하는 테이블들의 데이터가 동일한 행, 중복되는 데이터를 제외하고 모든 데이터를 출력함

            - 테이블 기본 구조 :

            - Mysql Query : 모든 SQL이 FULL OUTER JOIN을 지원하지는 않음

                - 기본 :

SELECT * FROM topic FULL OUTER JOIN author ON topic.author_id = author.id;

 

                - mysql : union은 중복을 제거해준다. union distinct에서 distinct는 생략 가능하다.

(SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.id)
UNION DISTINCT (SELECT * FROM topic RIGHT JOIN author ON topic.author_id = author.id;

            - 완성된 테이블 구조 :

    5) EXCLUSIVE JOIN

        1-1) 두개의 테이블 중 반대쪽 테이블에 있는 데이터를 제외하고 출력한다.

            - 테이블 기본 구조 :

            - Mysql Query : topic 테이블에서 author 데이터는 제외하고 출력한다.

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

            - 완성된 테이블 구조 :

댓글