본문 바로가기

DB

[DB] INNER JOIN

INNER JOIN

 

정의

    : INNER JOIN은 매칭 조건에 부합하는 행들만을 선택하여 결과로 제공합니다. 예를 들어, 두 테이블 A와 B가 있을 때, A의 특정 열과 B.   의 특정 열이 일치하는 경우에만 해당 행을 결과로 반환합니다.

특징

    - 가장 흔한 결합 방식.

aka

    : simple join

사용

    - n개의 테이블을 inner_join 할때, n-1개의 조건이 필요하다.

 

예제1

#사원의 이름과 그 사원이 속한 부서의 이름을 출력하시오.

# 명시적 inner join
select concat(employees.first_name, ' ', employees.last_name), departments.department_name from employees, departments where employees.department_id = departments.department_id;
# 암시적 inner join
SELECT concat(employees.first_name, ' ', employees.last_name), departments.department_name  FROM employees INNER JOIN departments ON employees.Department_ID = departments.Department_ID;

 

 

예제2

3개 이상의 inner join

 

#사원의 이름과 그 사원이 속한 부서가 속한 도시의 이름을 출력

select concat(e.first_name, ' ', e.last_name), l.city 
from employees e, departments d, locations l 
where e.department_id = d.department_id and d.location_id = l.location_id;

#또는
SELECT CONCAT(e.first_name, ' ', e.last_name), l.city
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
INNER JOIN locations l ON d.location_id = l.location_id;

'DB' 카테고리의 다른 글

[DB] SubQuery  (0) 2023.09.27
[DB] Outer join  (0) 2023.09.27
[DB] Natural join  (0) 2023.09.27
[DB] MySQL 함수들  (0) 2023.09.27
[DB] ERD (Lucidchart, MySQL-reverse engineering)  (0) 2023.09.27