본문 바로가기
IT Develop/SQL

MSSQL의 JOIN에 대하여 알아봅시다

by K-popcorn 2023. 4. 24.
반응형

마이크로소프트 SQL 서버(MSSQL)는 데이터를 저장, 검색 및 조작하는 데 사용되는 널리 사용되는 관계형 데이터베이스 관리 시스템입니다. MSSQL의 가장 중요한 기능 중 하나는 관련 열을 기준으로 둘 이상의 테이블에서 데이터를 결합할 수 있는 JOIN 연산입니다. 이 블로그에서는 MSSQL에서 JOIN을 사용하는 방법을 실제 사례와 함께 설명할 것입니다.

MSSQL의 JOIN 유형
MSSQL에는 네 가지 유형의 JOIN이 있습니다:

1. INNER JOIN
INER JOIN(EQUI JOIN이라고도 함)은 지정된 열을 기준으로 두 테이블에서 일치하는 값을 가진 행만 반환합니다. 예를 들어, "직원"과 "부서"라는 두 개의 테이블이 있다고 가정해 보겠습니다. 직원 테이블에는 "부서" 테이블의 "id" 열을 참조하는 "department_id" 열이 있습니다. 우리는 내부 조인을 사용하여 모든 직원과 해당 부서의 목록을 얻을 수 있습니다.

SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;


이 예에서 쿼리는 두 테이블 모두 department_id 및 id 값이 일치하는 경우에만 모든 직원의 이름과 해당 부서 이름을 반환합니다.

2. LEFT JOIN
LEFT JOIN은 왼쪽 테이블의 모든 행(JOIN 키워드 앞의 행)과 오른쪽 테이블의 일치하는 행(JOIN 키워드 뒤의 행)을 반환합니다. 오른쪽 테이블에 일치하는 행이 없으면 NULL 값이 반환됩니다. 예를 들어, "직원" 테이블과 "부서" 테이블이 동일하고 부서가 없는 직원을 포함하여 모든 직원과 해당 부서를 나열하려고 합니다.

SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;


이 예에서 쿼리는 모든 직원의 이름과 해당 부서 이름을 반환합니다. 직원에게 부서가 없는 경우 부서 이름에 대해 NULL 값이 반환됩니다.

3. RIGHT JOIN
RIGHT JOIN은 오른쪽 테이블의 모든 행과 왼쪽 테이블의 일치하는 행을 반환합니다. 왼쪽 테이블에 일치하는 행이 없으면 NULL 값이 반환됩니다. 예를 들어, "직원" 및 "부서" 테이블이 동일하고 직원이 없는 부서를 포함하여 모든 부서와 해당 직원을 나열하려고 합니다.

SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;


이 예에서 쿼리는 모든 부서 이름과 해당 직원 이름을 반환합니다. 부서에 직원이 없는 경우 직원 이름에 대해 NULL 값이 반환됩니다.

4. FULL OUTER JOIN
FULL OUTER JOIN은 일치하지 않는 행을 포함하여 두 테이블의 모든 행을 반환합니다. 행에 다른 테이블에 일치하는 행이 없으면 NULL 값이 반환됩니다. MSSQL에서는 "FULL OUTER JOIN" 키워드를 사용하여 이 작업을 수행할 수 있습니다. 예를 들어, "직원" 및 "부서" 테이블이 동일하고, 직원이 없는 부서와 부서가 없는 직원을 포함하여 모든 부서와 해당 직원을 나열하려고 합니다.

SELECT employees.name, departments.name
FROM employees
FULL OUTER JOIN departments ON employees.department_id = departments.id;


이 예에서 쿼리는 직원과 부서가 없는 직원을 포함하여 모든 부서 이름과 해당 직원 이름을 반환합니다.

 

(Eng.Ver)

Microsoft SQL Server (MSSQL) is a popular relational database management system used to store, retrieve, and manipulate data. One of the most important features of MSSQL is the JOIN operation, which allows you to combine data from two or more tables based on a related column. In this blog, we will explain how to use JOIN in MSSQL with practical examples.

Types of JOIN in MSSQL
There are four types of JOIN in MSSQL:

1. INNER JOIN
The INNER JOIN (also known as EQUI JOIN) returns only the rows that have matching values in both tables based on the specified column. For example, suppose we have two tables named "employees" and "departments". The "employees" table has a "department_id" column that references the "departments" table's "id" column. We can use INNER JOIN to get a list of all employees and their corresponding departments.

SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;


In this example, the query will return all the employees' names and their corresponding department names only if both tables have matching department_id and id values.

2. LEFT JOIN
The LEFT JOIN returns all the rows from the left table (the one before the JOIN keyword) and the matching rows from the right table (the one after the JOIN keyword). If there are no matching rows in the right table, NULL values will be returned. For example, let's say we have the same "employees" and "departments" tables, and we want to list all the employees and their corresponding departments, including those who don't have a department.

SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;


In this example, the query will return all the employees' names and their corresponding department names. If an employee doesn't have a department, NULL values will be returned for the department name.

3. RIGHT JOIN
The RIGHT JOIN returns all the rows from the right table and the matching rows from the left table. If there are no matching rows in the left table, NULL values will be returned. For example, let's say we have the same "employees" and "departments" tables, and we want to list all the departments and their corresponding employees, including those that don't have any employees.

SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;


In this example, the query will return all the department names and their corresponding employee names. If a department doesn't have any employees, NULL values will be returned for the employee name.

4. FULL OUTER JOIN
The FULL OUTER JOIN returns all the rows from both tables, including the non-matching rows. If a row doesn't have a matching row in the other table, NULL values will be returned. In MSSQL, we can use the keyword "FULL OUTER JOIN" to perform this operation. For example, let's say we have the same "employees" and "departments" tables, and we want to list all the departments and their corresponding employees, including those that don't have any employees and employees who don't have a department.

SELECT employees.name, departments.name
FROM employees
FULL OUTER JOIN departments ON employees.department_id = departments.id;


In this example, the query will return all the department names and their corresponding employee names, including those that don't have any employees and employees who don't have a department.

 

 

SQL JOIN

반응형

댓글