

 Amazon Redshift는 패치 198부터 새 Python UDF 생성을 더 이상 지원하지 않습니다. 기존 Python UDF는 2026년 6월 30일까지 계속 작동합니다. 자세한 내용은 [블로그 게시물](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)을 참조하세요.

# JOIN 예
<a name="r_Join_examples"></a>

SQL JOIN 절은 공통 필드를 기반으로 두 개 이상의 테이블에서 데이터를 결합하는 데 사용됩니다. 지정된 조인 메서드에 따라 결과가 변경될 수도 있고 변경되지 않을 수도 있습니다. JOIN 절의 구문에 대한 자세한 내용은 [파라미터](r_FROM_clause30.md#r_FROM_clause30-parameters) 섹션을 참조하세요.

다음 예에서는 `TICKIT` 샘플 데이터의 데이터를 사용합니다. 데이터베이스 스키마에 대한 자세한 내용은 [샘플 데이터베이스](c_sampledb.md) 섹션을 참조하세요. 샘플 데이터를 로드하는 방법을 알아보려면 Amazon Redshift 시작 안내서**의 [데이터 로드](https://docs.aws.amazon.com/redshift/latest/gsg/rs-gsg-create-sample-db.html) 단원을 참조하세요.

다음 쿼리는 LISTING 테이블과 SALES 테이블 간의 내부 조인(JOIN 키워드 사용 안 함)이며, 여기서 LISTING 테이블의 LISTID는 1에서 5 사이입니다. 이 쿼리는 LISTING 테이블(왼쪽 테이블) 및 SALES 테이블(오른쪽 테이블)에 있는 LISTID 열 값과 일치합니다. 결과는 LISTID 1, 4, 5가 조건과 일치한다는 것을 보여줍니다.

```
select listing.listid, sum(pricepaid) as price, sum(commission) as comm
from listing, sales
where listing.listid = sales.listid
and listing.listid between 1 and 5
group by 1
order by 1;

listid | price  |  comm
-------+--------+--------
     1 | 728.00 | 109.20
     4 |  76.00 |  11.40
     5 | 525.00 |  78.75
```

다음 쿼리는 왼쪽 외부 조인입니다. 왼쪽 및 오른쪽 외부 조인은 다른 테이블에서 일치 항목이 발견되지 않을 때 조인된 테이블 중 하나에서 값을 유지합니다. 왼쪽 및 오른쪽 테이블은 구문에 나열되는 첫 번째 및 두 번째 테이블입니다. NULL 값은 결과 집합의 "간격"을 채우는 데 사용됩니다. 이 쿼리는 LISTING 테이블(왼쪽 테이블) 및 SALES 테이블(오른쪽 테이블)에 있는 LISTID 열 값과 일치합니다. 결과는 LISTID 2 및 3이 어떤 판매로도 이어지지 않았음을 보여줍니다.

```
select listing.listid, sum(pricepaid) as price, sum(commission) as comm
from listing left outer join sales on sales.listid = listing.listid
where listing.listid between 1 and 5
group by 1
order by 1;

listid | price  |  comm
-------+--------+--------
     1 | 728.00 | 109.20
     2 | NULL   | NULL
     3 | NULL   | NULL
     4 |  76.00 |  11.40
     5 | 525.00 |  78.75
```

다음 쿼리는 오른쪽 외부 조인입니다. 이 쿼리는 LISTING 테이블(왼쪽 테이블) 및 SALES 테이블(오른쪽 테이블)에 있는 LISTID 열 값과 일치합니다. 결과는 LISTID 1, 4, 5가 조건과 일치한다는 것을 보여줍니다.

```
select listing.listid, sum(pricepaid) as price, sum(commission) as comm
from listing right outer join sales on sales.listid = listing.listid
where listing.listid between 1 and 5
group by 1
order by 1;

listid | price  |  comm
-------+--------+--------
     1 | 728.00 | 109.20
     4 |  76.00 |  11.40
     5 | 525.00 |  78.75
```

다음 쿼리는 전체 조인입니다. 전체 조인은 다른 테이블에서 일치 항목이 발견되지 않을 때 조인된 테이블의 값을 유지합니다. 왼쪽 및 오른쪽 테이블은 구문에 나열되는 첫 번째 및 두 번째 테이블입니다. NULL 값은 결과 집합의 "간격"을 채우는 데 사용됩니다. 이 쿼리는 LISTING 테이블(왼쪽 테이블) 및 SALES 테이블(오른쪽 테이블)에 있는 LISTID 열 값과 일치합니다. 결과는 LISTID 2 및 3이 어떤 판매로도 이어지지 않았음을 보여줍니다.

```
select listing.listid, sum(pricepaid) as price, sum(commission) as comm
from listing full join sales on sales.listid = listing.listid
where listing.listid between 1 and 5
group by 1
order by 1;

listid | price  |  comm
-------+--------+--------
     1 | 728.00 | 109.20
     2 | NULL   | NULL
     3 | NULL   | NULL
     4 |  76.00 |  11.40
     5 | 525.00 |  78.75
```

다음 쿼리는 전체 조인입니다. 이 쿼리는 LISTING 테이블(왼쪽 테이블) 및 SALES 테이블(오른쪽 테이블)에 있는 LISTID 열 값과 일치합니다. 판매로 이어지지 않는 행(LISTID 2 및 3)만 결과에 있습니다.

```
select listing.listid, sum(pricepaid) as price, sum(commission) as comm
from listing full join sales on sales.listid = listing.listid
where listing.listid between 1 and 5
and (listing.listid IS NULL or sales.listid IS NULL)
group by 1
order by 1;

listid | price  |  comm
-------+--------+--------
     2 | NULL   | NULL
     3 | NULL   | NULL
```

다음 예는 ON 절과의 내부 조인입니다. 이 경우 NULL 행은 반환되지 않습니다.

```
select listing.listid, sum(pricepaid) as price, sum(commission) as comm
from sales join listing
on sales.listid=listing.listid and sales.eventid=listing.eventid
where listing.listid between 1 and 5
group by 1
order by 1;

listid | price  |  comm
-------+--------+--------
     1 | 728.00 | 109.20
     4 |  76.00 |  11.40
     5 | 525.00 |  78.75
```

다음 쿼리는 결과를 제한하는 술어가 있는, LISTING 테이블과 SALES 테이블의 교차 조인 또는 데카르트 조인입니다. 이 쿼리는 SALES 테이블과 LISTING 테이블의 LISTID(두 테이블 모두 LISTID 1, 2, 3, 4, 5) 열 값과 일치합니다. 결과는 20개의 행이 조건과 일치한다는 것을 보여줍니다.

```
select sales.listid as sales_listid, listing.listid as listing_listid
from sales cross join listing
where sales.listid between 1 and 5
and listing.listid between 1 and 5
order by 1,2;

sales_listid | listing_listid
-------------+---------------
1            | 1
1            | 2
1            | 3
1            | 4
1            | 5
4            | 1
4            | 2
4            | 3
4            | 4
4            | 5
5            | 1
5            | 1
5            | 2
5            | 2
5            | 3
5            | 3
5            | 4
5            | 4
5            | 5
5            | 5
```

다음 예는 두 테이블 간의 자연 조인입니다. 이 경우 listid, sellerid, eventid, dateid 열은 두 테이블 모두에서 동일한 이름과 데이터 형식을 가지므로 조인 열로 사용됩니다. 결과는 5개 행으로 제한됩니다.

```
select listid, sellerid, eventid, dateid, numtickets
from listing natural join sales
order by 1
limit 5;

listid | sellerid  | eventid | dateid | numtickets
-------+-----------+---------+--------+-----------
113    | 29704     | 4699    | 2075   | 22
115    | 39115     | 3513    | 2062   | 14
116    | 43314     | 8675    | 1910   | 28
118    | 6079      | 1611    | 1862   | 9
163    | 24880     | 8253    | 1888   | 14
```

다음 예는 USING 절을 사용한 두 테이블 간의 조인입니다. 이 경우 listid 및 eventid 열이 조인 열로 사용됩니다. 결과는 5개 행으로 제한됩니다.

```
select listid, listing.sellerid, eventid, listing.dateid, numtickets
from listing join sales
using (listid, eventid)
order by 1
limit 5;

listid | sellerid | eventid | dateid | numtickets
-------+----------+---------+--------+-----------
1      | 36861    | 7872    | 1850   | 10
4      | 8117     | 4337    | 1970   | 8
5      | 1616     | 8647    | 1963   | 4
5      | 1616     | 8647    | 1963   | 4
6      | 47402    | 8240    | 2053   | 18
```

다음 쿼리는 FROM 절에 있는 두 하위 쿼리의 내부 조인입니다. 다음 쿼리는 다양한 범주의 이벤트(콘서트 및 쇼)에 대해 판매된 티켓과 판매되지 않은 티켓의 수를 찾습니다. 이러한 FROM 절 하위 쿼리는 *table* 하위 쿼리로서, 여러 개의 열과 행을 반환할 수 있습니다.

```
select catgroup1, sold, unsold
from
(select catgroup, sum(qtysold) as sold
from category c, event e, sales s
where c.catid = e.catid and e.eventid = s.eventid
group by catgroup) as a(catgroup1, sold)
join
(select catgroup, sum(numtickets)-sum(qtysold) as unsold
from category c, event e, sales s, listing l
where c.catid = e.catid and e.eventid = s.eventid
and s.listid = l.listid
group by catgroup) as b(catgroup2, unsold)

on a.catgroup1 = b.catgroup2
order by 1;

catgroup1 |  sold  | unsold
----------+--------+--------
Concerts  | 195444 |1067199
Shows     | 149905 | 817736
```