INNER JOIN의 핵심 이해

SQL에서 데이터 조인하기

Maham Faisal Khan

Senior Content Developer, DataCamp

INNER JOIN의 핵심 이해

A diagram showing two tables: left table and right table. Matching id values in left and right table are displayed using the same colors.

SQL에서 데이터 조인하기

INNER JOIN의 핵심 이해

  • INNER JOIN은 지정된 필드에서 일치하는 두 테이블의 레코드를 찾음
id 필드에 대한 INNER JOIN 다이어그램

A diagram showing two tables: left_table and right_table. Matching id values in the left and right table are displayed using the same colors and are connected with arrows.

SQL에서 데이터 조인하기

INNER JOIN의 핵심 이해

id 필드에 ON을 사용하는 INNER JOIN 다이어그램

A diagram showing two tables: left_table and right_table. Matching id values in left and right table are displayed using the same colors and connected with arrows. Records that are not of interest to INNER JOIN have been faded out.

SQL에서 데이터 조인하기

INNER JOIN의 핵심 이해

INNER JOIN 필드에 ON을 사용하는 id 다이어그램

A diagram showing two tables: left_table and right_table. The result of joining the two queries is shown on the right, returning only the records that matched on the id column for both tables.

SQL에서 데이터 조인하기

leadership 데이터베이스 스키마

2024년까지의 세계 지도자: A SQL schema showing three tables in the leadership database: presidents, prime_ministers, and prime_minister_terms. presidents and prime_minister can be joined on the country field, whereas prime_ministers and prime_minister terms can be joined on the prime_minister field.

SQL에서 데이터 조인하기

presidents 테이블 살펴보기

SELECT * 
FROM presidents;
| country  | continent     | president               |
|----------|---------------|-------------------------|
| Egypt    | Africa        | Abdel Fattah el-Sisi    |
| Portugal | Europe        | Marcelo Rebelo de Sousa | 
| USA      | North America | Joe Biden               |
| Uruguay  | South America | Luis Lacalle Pou        |
| Pakistan | Asia          | Asif Ali Zardari        |
| Chile    | South America | Gabriel Boric           |
| India    | Asia          | Droupadi Murmu          |
SQL에서 데이터 조인하기

총리들을 소개합니다

prime_ministers 테이블

The prime_ministers table from the leadership database, containing three columns: country, continent and prime_minister.

SQL에서 데이터 조인하기

총리와 대통령을 한 테이블에서

presidents 테이블

Two tables shown side-by-side: presidents and prime_minister. Matching columns are colored in the two tables.

prime_ministers 테이블

Two tables shown side-by-side: presidents and prime_minister. Matching columns are colored in the two tables.

SQL에서 데이터 조인하기

첫 번째 INNER JOIN

--Inner join of presidents and prime_ministers, joining on country

SELECT prime_ministers.country, prime_ministers.continent, prime_minister, president
FROM presidents
INNER JOIN prime_ministers
ON presidents.country = prime_ministers.country;

참고. 두 테이블에 모두 존재하는 열을 선택할 때는 SQL 오류 방지를 위해 table.column_name 형식을 사용해야 함.

| country        | continent | prime_minister   | president               |
|----------------|-----------|------------------|-------------------------|
| Egypt          | Africa    | Mostafa Madbouly | Abdel Fattah el-Sisi    |
| Portugal       | Europe    | Luís Montenegro  | Marcelo Rebelo de Sousa |
| Pakistan       | Asia      | Shehbaz Sharif   | Asif Ali Zardari        |
| India          | Asia      | Narendra Modi    | Ram Nath Kovind         |
SQL에서 데이터 조인하기

테이블 별칭 지정하기

--Inner join of presidents and prime_ministers, joining on country

SELECT p2.country, p2.continent, prime_minister, president
FROM presidents AS p1 INNER JOIN prime_ministers AS p2
ON p1.country = p2.country;
| country        | continent | prime_minister   | president               |
|----------------|-----------|------------------|-------------------------|
| Egypt          | Africa    | Mostafa Madbouly | Abdel Fattah el-Sisi    |
| Portugal       | Europe    | Luís Montenegro  | Marcelo Rebelo de Sousa |
| Pakistan       | Asia      | Shehbaz Sharif   | Asif Ali Zardari        |
| India          | Asia      | Narendra Modi    | Ram Nath Kovind         |

별칭은 SELECTON 절에서 table.column_name 구문에 사용이 가능함.

SQL에서 데이터 조인하기

USING 사용하기

--Inner join of presidents and prime_ministers, joining on country
SELECT p2.country, p2.continent, prime_minister, president
FROM presidents AS p1
INNER JOIN prime_ministers AS p2
USING(country);
| country        | continent | prime_minister   | president               |
|----------------|-----------|------------------|-------------------------|
| Egypt          | Africa    | Mostafa Madbouly | Abdel Fattah el-Sisi    |
| Portugal       | Europe    | Luís Montenegro  | Marcelo Rebelo de Sousa |
| Pakistan       | Asia      | Shehbaz Sharif   | Asif Ali Zardari        |
| India          | Asia      | Narendra Modi    | Ram Nath Kovind         |
SQL에서 데이터 조인하기

연습해봅시다!

SQL에서 데이터 조인하기

Preparing Video For Download...